Tag: 承诺

使用承诺而不是事件callback

我有以下的代码,这个所有的代码是在指定的函数myFunc ,我需要的所有function将完成(myFunc),即当文件成功提取/或不返回一些状态(成功/错误) var myFunc = () => { var DecompressZip = require('decompress-zip'); var unzipper = new DecompressZip(filename) unzipper.on('error', function (err) { console.log('Caught an error'); }); unzipper.on('extract', function (log) { console.log('Finished extracting'); }); unzipper.on('progress', function (fileIndex, fileCount) { console.log('Extracted file ' + (fileIndex + 1) + ' of ' + fileCount); }); unzipper.extract({ path: 'some/path', filter: […]

循环之后的节点/ mongo响应

当循环完成后,我将如何返回总数作为parsing(总数)? 怎么办呢? function getTotalAttack(minions) { return new Promise( function (resolve, reject) { console.log("starting do the totalattack math"); var total = 0; for(var minion in minions) { console.log("sending one now"); minions.findOne({ _Id : minion }).then(function(response){ total = total + response.attack; }); } console.log("result is " + total); return resolve(total); }); }

Nodejs蓝鸟诺言在处理图像时失败

//Created a promise for each image size. var promises = sizes.map(function (size) { return new Promise(function (resolve, reject) { var destinationDir = fileUtil.getAbsolutePathOfImage(destinationPath); fileUtil.createDirectoryIfNotExists(destinationDir); destinationDir += size.src; fileUtil.createDirectoryIfNotExists(destinationDir); //Resize the image. //console.log('imagefile : ' + JSON.stringify(imageFile)); //console.log('destinationDir: ' + JSON.stringify(destinationDir)); //Called an imageUtil resize method to perform resize. imageUtil.resize(imageFile, destinationDir, size).then(data => { var fileName […]

在链中捕获,但使用节点6获得UnhandledPromiseRejectionWarning

我有这个承诺链。 com.openPort(port).then(port => { _.pTimeout(3000, com.sendPort(port, NCD.gen(args.cmd))) .then(received => { console.log('complete response: ', NCD.parse(received)); Debug.L1('resolved num data listeners: ', port.listenerCount("data")); }) }) .catch(function(e) { console.log('error: ', e) }); 我正在通过发送一个伪造的命令来testing超时的承诺,这将使sendPort的承诺得不到解决。 超时承诺赢得比赛投掷拒绝,但警告之前.catch处理拒绝。 这里是超时承诺比赛 pTimeout: function(timeout, promise) { return Promise.race([ promise, new Promise(function(resolve, reject) { setTimeout(function() { reject('\nTimed out'); }, timeout); }) ]); } 控制台显示 (node:9616) UnhandledPromiseRejectionWarning: Unhandled […]

Node.js中的未处理诺言拒绝

我试图做一个DELETE调用,我正在实现下面的function。 我明白,在一个承诺,需要有一个决心和拒绝的状态,但我得到一个未处理的承诺拒绝错误: UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):[object Object] 我不太喜欢在promise中使用条件语句,因为它会变得混乱,但是我在这里要做的是检查组织是否被validation,如果是的话,删除操作不应该发生并且会被拒绝。 function deleteOrg(id) { return new Promise((resolve, reject) => { //A helper function that return an 'org' object findById(id) .then((orgObject) => { if (orgObject.verified_at !== null) { throw new Error(422, 'Unable to delete organization') } //Organization is not verified, so proceed to delete new Organization().where({'id': id}).destroy() .then(() => { return resolve() […]

如何使用Node Js Promise按顺序执行函数?

我是新来Node js Promise我不确定我是否正确使用Promise,所以这是我的代码。 function print(){ first('first') .then(second('second')) .then(third('third')); } function first(a){ return new Promise((resolve, reject) => { var res1 = function (){ resolve(a); } }); console.log(a); } function second(b){ return new Promise((resolve, reject) => { var res1 = function (){ resolve(b); } }); setTimeout(() => { console.log(b); }, 2000); } function third(c){ return new Promise((resolve, […]

mongoose.connection.collections.collection.drop()每隔一次都会抛出错误

我正在使用Jest为Node / Express / Mongo项目设置testing。 我试图写一个函数来清除集合,所以每个testing都以一个干净的平板开始: const clearCollection = (collectionName, done) => { const collection = mongoose.connection.collections[collectionName] collection.drop(err => { if (err) throw new Error(err) else done() ) } beforeEach(done => { clearCollection('users', done) }) 另一个尝试,承诺: const clearCollection = collectionName => { const collection = mongoose.connection.collections[collectionName] return collection.drop() } beforeEach(async () => { await clearCollection('users') […]

关于Node.js Promise然后返回?

我对Promise感到困惑! 我使用Promise然后没有像这样回报: new Promise((resolve, reject) => { resolve("1"); }).then((v1) => { console.log("v1"); new Promise((resolve, reject) => { //Time-consuming operation, for example: get data from database; setTimeout(() => { resolve(2) }, 3000); }).then((v11) => { console.log("v11"); }) }).then((v2) => { console.log("v2") }); 我得到这个结果v1 v2 v11 。 然后,我用另一种方式写,如下所示: new Promise((resolve, reject) => { resolve("1"); }).then((v1) => { […]

蓝鸟promisifyAll与promisifying每种方法

我正在使用节点模块而不是使用PromisifyAll()我每次当我调用该模块的方法创build一个新的承诺对象。 这是一个安全的方式promisify? 如果没有,那么我的整个代码结构将会改变。 所以改变或者只要我使用Promise就很好。 如果我单独使用每种方法,是否对内存或CPU有影响?

在promise中定义asynchronous函数

我正在使用.then编写承诺代码。 现在,我决定使用await / async来编写它。 我已经在promise中调用了一个函数add_Lessons ,然后在那个函数中调用另一个函数。 这是我的代码使用.then 。 function create_section(sections,course_ID,i) { return new Promise( (resolve) => { var s_duration = 0; var sname = sections[i].name; var s_obj = { //some object; } var section_id; DB.section.create(s_obj,function (err, data_s) { if (err) return next(err); section_id = data_s._id; var lesson = sections[i].lessons; add_lessons(lesson,section_id,i) .then(function(arr){ resolve(arr); }) }); } […]