我正在使用Q promise库.我的代码依赖于单个promise的回调按照它们注册的顺序执行的事实.
var deferred = Q.defer();
var promise = deferred.promise;
['first', 'second', 'third'].forEach(function (position) {
promise.then(function () {
alert(position);
});
});
deferred.resolve();
Run Code Online (Sandbox Code Playgroud)
这确实产生了正确的结果,但我不知道它是否是规范的一部分或者是一个可以打破界限的幸福巧合.
考虑我有一系列对象和承诺,例如:
[{
a: 1
}, {
a: 4
}, {
a: 4
}, {
promiseSend: [Function],
valueOf: [Function]
}, {
promiseSend: [Function],
valueOf: [Function]
}]
Run Code Online (Sandbox Code Playgroud)
现在,当调用I Q.all(arr)并返回对象值时then(),没有任何事情发生,仍然我的数组包含promise对象.有没有办法使用Q.all()这样一个复杂的数组?
Angular 1.2取代always了finally承诺.
那么曾经是这样的:
$http.get('/myurl').always(handler);
Run Code Online (Sandbox Code Playgroud)
现在需要这样:
$http.get('/myurl').finally(handler);
Run Code Online (Sandbox Code Playgroud)
但我在IE8中收到错误"预期标识符".如何在IE8中完成这项工作?
我试图了解承诺链是如何工作的.我使用q.js.这就是我正在玩的东西.
var Q = require("q"); // npm install q
// the function Q(value) returns a fulfilled promise with the value... I think.
Q(1).then(Q(2)).then(Q(3)).then(Q(4)).then(function(n) {
console.log(n);
});
Q(1).then(function(n) {
return Q(2);
}).then(function(n) {
return Q(3);
}).then(function(n) {
return Q(4);
}).then(function(n) {
console.log("done: " + n);
});
Run Code Online (Sandbox Code Playgroud)
我的问题基本上归结为为什么第一个日志记录1而后者记录我期望的内容并且基本上记录1到4.我曾希望第一个记录4而不是1.
我真的只是希望能够有一些返回promises的方法,然后像瀑布一样将它们链接在一起 - 我想我可以使用异步和瀑布,但只是想知道这是否可以实现w/promises.
如何编写限制Q promise并发的方法?
例如,我有一个方法spawnProcess.它返回一个Q承诺.
我希望一次生成的进程不超过5个,但对调用代码透明.
我需要实现的是一个带签名的函数
function limitConcurrency(promiseFactory, limit)
Run Code Online (Sandbox Code Playgroud)
我可以这样打电话
spawnProcess = limitConcurrency(spawnProcess, 5);
// use spawnProcess as usual
Run Code Online (Sandbox Code Playgroud)
我已经开始研究我的版本,但我想知道是否有人有一个我可以检查的简洁实现.
作为我正在编写的小程序的一部分,我想使用gulp将大量文件转换为markdown.这不是与程序分开的构建步骤的一部分.这是该计划的一部分.所以我没有使用gulpfile来处理这个问题.
问题是,因为它是异步的,我想使用一个在gulp任务完成时会提醒我的promise.
这样的事情是理想的:
io.convertSrc = function() {
var def = q.defer();
gulp.src(src + '/*.md')
.pipe(marked({}))
.pipe(gulp.dest(dist), function() {
def.resolve('We are done!');
});
return def.promise;
}
Run Code Online (Sandbox Code Playgroud)
但是pipe不接受回调.我怎么能处理这个?谢谢你的帮助,我有点新意.
所以我有一个像这样的字符串 C3H20IO
我想做的是分割这个字符串,所以我得到以下内容:
Array1 = {C,H,I,O}
Array2 = {3,20,1,1}
Run Code Online (Sandbox Code Playgroud)
的1作为对所述第三元件Array2是指示所述的单原子性质I元件.同样的O.这实际上是我正在努力的部分.
这是一个化学方程,所以我需要根据它们的名称和原子数等来分离元素.
使用mongoose来查询db和Q for promises的结果,但是发现很难找到一个可用的用户列表.目前我有这样的事情:
var checkForPerson = function( person ) {
people = mongoose.model('Person', Person)
return people.findOne({"_id": person }, function(err, doc) {
if (err) console.log(err)
if (doc !== null) {
return doc
} else {
console.log('no results')
}
})
}
var promises = someArrayOfIds.map(checkForPerson);
// this is where I would like to have an array of models
var users = Q.all(promises)
//this fires off before the people.findOne query above to users is undefined
SomeOtherFunction( users )
Run Code Online (Sandbox Code Playgroud)
如果SomeOtherFunction没有做大量的草率回调,我将如何完成查询?
鉴于以下功能:
function isGood(number) {
var defer = $q.defer();
$timeout(function() {
if (<some condition on number>) {
defer.resolve();
} else {
defer.reject();
}
}, 100);
return defer.promise;
}
Run Code Online (Sandbox Code Playgroud)
和一组数字(例如[3, 9, 17, 26, 89]),我想找到第一个 "好"的数字.我希望能够做到这一点:
var arr = [3, 9, 17, 26, 89];
findGoodNumber(arr).then(function(goodNumber) {
console.log('Good number found: ' + goodNumber);
}, function() {
console.log('No good numbers found');
});
Run Code Online (Sandbox Code Playgroud)
这是一个可能的递归版本来实现这个: DEMO
function findGoodNumber(numbers) {
var defer = $q.defer();
if (numbers.length === 0) {
defer.reject();
} else { …Run Code Online (Sandbox Code Playgroud) 在我的场景中,当我提出请求时,我会返回一个承诺.
最后我解决/拒绝延期的obj.
如果尚未解决/拒绝,我想重复使用该承诺.
任何信息都会有用.