超越简单承诺的任何事情通常让我感到困惑.在这种情况下,我需要在N个对象上连续进行2次异步调用.首先,我需要从磁盘加载文件,然后将该文件上载到邮件服务器.我更愿意一起完成这两个动作,但是我已经通过先完成所有读操作然后再次上传所有内容来实现它.下面的代码有效,但我不禁想到它可以做得更好.有一点我不明白为什么when.all不拒绝.我对文档的解释似乎暗示,如果其中一个承诺拒绝,那么.all将拒绝.为了测试错误,我已经注释掉了较低的分辨率.没有错误,事情似乎工作得很好并且有意义.
mail_sendOne({
from: 'greg@',
to: 'wilma@',
subject: 'F&B data',
attachments: [
{name: 'fred.html', path: '/fred.html'},
{name: 'barney.html', path: '/barney.html'}
]
})
.done(
function(res) {
console.log(res)
},
function(err) {
console.log('error ', err);
}
)
function mail_sendOne(kwargs) {
var d = when.defer();
var promises = [], uploadIDs = [], errs = [];
// loop through each attachment
for (var f=0,att; f < kwargs.attachments.length; f++) {
att = kwargs.attachments[f];
// read the attachment from disk
promises.push(readFile(att.path)
.then(
function(content) {
// upload attachment to …Run Code Online (Sandbox Code Playgroud) 关于这个jsFiddle,我试图动态添加一个事件触发时创建的"延迟",所以只有当所有延迟被解析时才会调用完成的回调,包括后来添加的那些:
相关代码:
var promises = [ deferred1, ... ];
var p = when.all(promises).then(function() {
console.log('All done!!');
//! trigger
});
promises.push( deferredFromEvent ); // << ignored
Run Code Online (Sandbox Code Playgroud)
更新:欢迎使用Q或jQuery的建议,我正在寻找一个有效的建议
我想陷阱when.js未处理的拒绝,以便我可以记录它们.为了实现这一点,我已经覆盖了console.warn(),但是这可以记录除了我不感兴趣的when.js之外的东西.
参考:https://github.com/cujojs/when/blob/master/docs/api.md#debugging-promises
我正在使用prettymonitor with when.js https://github.com/AriaMinaei/pretty-monitor
我正在使用Mocha.js的测试设置和测试中的许多承诺.测试依赖于在DOM中设置内容,在测试之间,DOM被清除.但是,有时测试运行缓慢且超时.在这种情况下,它们的promise会继续执行,但DOM会在下一次测试之前被清除,因此promise可能会错误地将错误输入到下一个测试中.有没有办法取消或销毁测试之间所有未完成的承诺?我们正在使用when.js的承诺.
我想使用该when.map函数来处理一些数据.在处理数据之后,我需要进行一些清理(例如,将当前使用的数据库连接释放回连接池).
用我的方法的问题catch,并finally 是finally首当被调用reject时,而其他,和映射仍在进行中.
那么我怎么能等到所有的映射承诺都完成,这样才能进行保存清理.
require('when/monitor/console');
var when = require('when');
function testMapper(value) {
console.log('testMapper called with: '+value);
return when.promise(function(resolve, reject) {
setTimeout(function() {
console.log('reject: '+value);
reject(new Error('error: '+value));
},100*value);
});
}
when.map([1,2,3,4],testMapper)
.then(function() {
console.log('finished')
})
.catch(function(e) {
console.log(e);
})
.finally(function() {
console.log('finally')
});
Run Code Online (Sandbox Code Playgroud)
产量
testMapper called with: 1
testMapper called with: 2
testMapper called with: 3
testMapper called with: 4
reject: 1
[Error: error: 1]
finally
reject: …Run Code Online (Sandbox Code Playgroud) 我正试图解除AWS S3异步功能,并遇到一个奇怪的错误.鉴于以下代码,
var s3 = new AWS.S3();
var when = require('when');
var nodefn = require('when/node');
var getObjectP = nodefn.lift(s3.getObject);
getObjectP({
Bucket: 'bucket_name',
Key: 'key_name'
})
.then(function(data) {
...
}, function(err) {
...
});
Run Code Online (Sandbox Code Playgroud)
我收到这个错误,
Object #<Object> has no method 'makeRequest'
Run Code Online (Sandbox Code Playgroud)
这是getObject通常的样子(当我使用回调而不是promises时,它工作正常):
s3.getObject({ ... }, function(err, data) {
...
});
Run Code Online (Sandbox Code Playgroud)
我在滥用nodefn.lift吗?这看起来很简单.这是所有感兴趣的人的文档.https://github.com/cujojs/when/blob/master/docs/api.md#nodelift
有人可以解释为什么这个以相反的顺序打印?
码:
when('test')
.then(function() {console.log('should be first');})
.then(console.log('should be second'));
Run Code Online (Sandbox Code Playgroud)
输出:
should be second
should be first
Run Code Online (Sandbox Code Playgroud)
PS:我使用的是when.js版本:when@3.4.3