co.wrap(function * (libTplPath, localTplPath) { let tpl try { yield thenFs.stat(localTplPath) tpl = yield thenFs.readFile(localTplPath, 'utf-8') } catch (e) { tpl = yield thenFs.readFile(libTplPath, 'utf-8') yield thenFs.writeFile(localTplPath, tpl) } return tpl })
describe('Cursors', () => { it('should work with no suffix', co.wrap(function * () { const db = datastore('') yield db.insert([ { id: 2, name: 'Tim' }, { id: 1, name: 'Tom' } ]) const res = yield db.cfind().sort({ name: -1 }).projection({ name: 1 }).exec() assert.equal(res[0].name, 'Tom') assert.equal(res[1].name, 'Tim') assert.equal(typeof res[0].id, 'undefined') })) })
it('can be wrapped to return a promise', function(done) { class Class {} const ashley = new Ashley(); const fn = co.wrap(ashley.instance('Instance', Class)); fn().then(instance => { expect(instance).to.be.an.instanceof(Class); done(); }); });
co.wrap(function * (acLists, numObj) { yield resultUtils.getResult() return acLists.filter(element => { if (element['paid_only']) { leetcodeNumObj.locked++ } //! (element.stat['question_id'] in result) // if we fetched the problem before, we will not do it again return element.status === 'ac' && (element.stat['question_id'] in numObj) }).reverse() })
http.createServer(function (req, res) { var _parseBody = co.wrap(parseBody); _parseBody(req).then(onbody, onerror); function onbody(body) { var keys = Object.keys(body); res.end(keys.join(',')); } function onerror(err) { res.statusCode = 500; res.end(err.message); } }).listen(3000);
co.wrap(function *(query){ connection = new mssql.Connection(config); yield connection.connect(); var request = new mssql.Request(connection); var recordset = yield request.query(query); return recordset; })
set action (action) { if (Util.isGeneratorFunction(action)) { this.actMeta.action = Co.wrap(action); this.isPromisable = true; } else if (Util.isAsyncFunction(action)) { this.actMeta.action = action; this.isPromisable = true; } else { this.actMeta.action = action; this.isPromisable = false; } }
co.wrap(function * (problemInfo, languageCodeMap) { let responseAndBody try { responseAndBody = yield request.get('/problems/' + problemInfo.stat['question__title_slug'] + '/') } catch (err) { debug('network error:error happened when fetching problem \'' + problemInfo.stat['question__title_slug'] + '\'') throw err }; let $ = cheerio.load(responseAndBody[1]) // debug('problem :'); // debug($('meta[name=description]').attr('content')); languageCodeMap['_question'] = $('meta[name=description]').attr('content') })
describe('#count()', () => { it('should return the number of documents in the database', co.wrap(function * () { let count = yield DB.count({}) assert.equal(count, 3) })) it('should work with cursors', co.wrap(function * () { const count = yield DB.ccount({}).limit(2).exec() assert.equal(count, 2) })) })
co.wrap(function * () { debug('fetch the accepted solutions\' list') let [, body] = yield request.get('/api/problems/algorithms/') try { body = JSON.parse(body) } catch (e) { debug(body) throw (new Error('network error:JSON data error')) } leetcodeNumObj.total = body['num_total'] leetcodeNumObj.solved = body['num_solved'] leetcodeNumObj.locked = 0 return body['stat_status_pairs'] })
co.wrap(function* (docId) { //todo redis ? var res = false; var removeRes = yield taskResult.remove(docId); if (removeRes.affectedRows > 0) { yield storage.deletePath(docId); res = true; } return res; })
describe('#remove()', () => { it('should remove a document', co.wrap(function * () { let beforeCount = yield DB.count({}) yield DB.remove({}) let afterCount = yield DB.count({}) assert.equal(beforeCount - afterCount, 1) })) it('should remove all documents', co.wrap(function * () { yield DB.remove({}, { multi: true }) let afterCount = yield DB.count({}) assert.equal(afterCount, 0) })) })
co.wrap(function * (pathToWrite, content) { yield thenFs.writeFile(pathToWrite, content).catch(err => { throw err }) })
http.createServer(function (req, res) { var _parseBody = co.wrap(parseBody); _parseBody(req).then(onbody, onerror); function onbody(body) { var keys = Object.keys(body); res.end(keys.join(',')); } function onerror(err) { res.statusCode = 500; res.end(err.message); } }).listen(3000);
co.wrap(function * (acLists) { // load the last spider result from result.json // if result.json doesn't exist that means never fetched solutions before let result = yield resultUtils.getResult(resultJsonPath) return acLists.filter(element => { if (element['paid_only']) { leetcodeNumObj.locked++ } //! (element.stat['question_id'] in result) // if we fetched once we will not fetch this problem any more return element.status === 'ac' && !(element.stat['question_id'] in result) }).reverse() })