Tag: 承诺

链接asynchronousfunction?

let val = 0; async function first() { console.log('1a', val); second(); console.log('1b', val); } async function second() { console.log('2a', val); third(); console.log('2b', val); } async function third() { console.log('3a', val); val = await new Promise(function (resolve, reject) { setTimeout(function () { resolve(3); }, 1000); }); console.log('3b', val); } console.log('0a', val); first(); console.log('0b', val); 我期待: 0a […]

如何找出未处理的承诺拒绝发生在哪里?

快速序言:我了解承诺,我明白解决和拒绝。 这不是问题。 (node:14104) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: "callback" argument must be a function (node:14104) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.\ 我理解我的代码中的一个“callback”参数,这应该是一个函数,不是。 这也不是我的问题。 如何find未处理的拒绝发生的代码行?

Node.js:在MongoDb中使用Promise

我已经开始在简单的node.js应用程序中使用Q Promise包。 所以我感兴趣的是如何在所有承诺序列完成后closures数据库连接。 例: var toDbConnectionString = function(dbSettings) { return "mongodb://" + dbSettings.user + ":" + dbSettings.password + "@" + dbSettings.url; }; var connectionString = toDbConnectionString(dbSettings); Q.nfcall( MongoClient.connect, toDbConnectionString(dbSettings)) .then(function(db) { return Q.ninvoke(db, "collectionNames"); }) .then(function(collections) { console.log(collections); }) .catch(function() { console.log(arguments); }); 我想在显示集合名称后closures连接,但在这个匿名函数中没有数据库上下文。 有没有办法如何处理这种承诺模式的情况?

Promise.all与嵌套的Promise.all

我嵌套数组,我能够检索第二级数组的承诺,但不知道如何执行一次, then顶级完成以及。 result.forEach(function(entity){ // outer list ??? return Promise.all(entity.urls.map(function(item){ return requestURL(item.href); })); }); 例如,如果results有两个或更多的项目,每个item有10个或更多的url需要获取,那么我们如何实现[Promise.all][1]所有的promise。 请原生解决scheme。 基本上以正确的方式处理嵌套数组的承诺。 数据结构: var result = [ { urls: [ {href: "link1"}, {href: "link2"}, {href: "link3"} ] }, { urls: [ {href: "link4"}, {href: "link5"}, {href: "link6"} ] } ];

JS:async.each的等价承诺是什么?

似乎今天宣布,承诺将在ES6。 我从来没有成为一个承诺的家伙,.then()的史诗链似乎比async.waterfall()中的一个简单的函数列表更复杂,但是看起来像我将不得不去学习它们。 那么,其他非常stream行的工作streamasync.each()是什么呢? async.each(items, processItem, function(err){ doSomething(err) }); 例如,在每个项目上运行processItem函数,一旦完成,继续执行doSomething(如果任何processItem()被搞砸,则执行一些不同的操作)。 我如何在承诺中做到这一点? 是否有一个官方的承诺用户文档的地方(不是诺言规范 ,实际示例工作stream程,以及如何做承诺)像asynchronous呢?

蓝鸟的Promise.settle不能用正确的值解决

我有以下代码: return Promise.settle(matches, imgur.uploadUrl) .map(function (inspection) { if (inspection.isFulfilled()) { return inspection.value().data.link; } return '#'; }) 上面的更详细的版本显示相同的问题: return Promise.settle(matches, function(match) { return imgur.uploadUrl(match); }) .then(function(results) { return results; }) .map(function (inspection) { if (inspection.isFulfilled()) { return inspection.value().data.link; } return '#'; }) 哪里 Promise =蓝鸟的承诺 matches =从string中提取的图像链接数组 imgur = https://github.com/kaimallea/node-imgur 预期的行为是.map的结果是在原始数组中的图像被上传到imgur(或'#',在上传失败的情况下由于任何原因)之后,用imgur链接数组解决的承诺。 相反,Promise.settle 立即解决(即似乎不等待imgur上传),而inspection.value()是来自matches数组的原始图像url(当尝试读取数据时会产生错误) .data.link一个string的.data.link属性)。 为什么会发生? 为什么不能上传到imgur并正确parsing?

传播承诺的正确方法是什么?

我正在使用蓝鸟来提供mongoose库。 所以,我现在find并保存数据如下: User.findOneAsync({email: req.body.text}) .then(function(user) { user.saveAsync() .spread(function(savedUser){ res.json(savedUser); }) .catch(function(err) { res.json({ status: 500, message: 'foo' }); }); }) .catch(function(err) { res.json({ status: 500, message: 'foo' }); }); 两个捕获函数是完全相同的。 这只是一个演示,我有时在实际工作中有两个相同的捕捉function。 我可以把catch里面的函数分解成它自己的函数。 不过,我不得不多次编写catch函数。 什么是避免重复捕捉function的好方法? 任何帮助,将不胜感激。

链接承诺不使用多个“那么”

我正在学习如何使用Promise。 我有以下函数返回“i”xkcd漫画标题作为承诺: var xkcd = function(i) { return new Promise( function(resolve, reject) { var tempurl = 'https://www.xkcd.com/' + i; request(tempurl, function(error, response, body) { if (error) reject(error); var $ = cheerio.load(body); resolve($('title').text() + '\n'); }); }); }; 如果我想要得到前4个冠军,我就把我的.then()这样链接起来: var result = ''; xkcd(1) .then(fullfilled => { result += fullfilled; }) .then(() => xkcd(2)) .then(fullfilled => […]

Node.js – 在另一个方法完全执行后调用一个方法

我有两个简单的方法: function do(x,y){ if(x){ XHR1().success(); } if(y){ XHR2().success(); } } function done(){ return something; } 现在我只想确保在do()完成时调用done() (** do()方法包含Mysql DB的asynchronous请求 ) 我怎么能做到这一点?** 显然这不会把这样的方法按顺序排列: do(x=1,y=1); done(); //this can run also if do() has not finished yet 所以我tryed: function do(x,y,_callback){ if(x){ XHR1().success(); } if(y){ XHR2().success(); } _callback(); } function done(){ return something; } do(x=1,y=1,done()); // this won't work […]

承诺链内循环

for (var i in listofInstances) { cleanupInstance(listofInstances[ i ]) .then(function () { console.log("Done" + listofInstances[ i ]) }); } cleanupInstance也是一个承诺链。 然而,目前我的for循环在完成整个承诺链之前进入下一个迭代。 有没有办法让promisify这个循环呢? 我正在使用蓝鸟库(nodejs)承诺。