打电话承诺然后毫不延迟
Call to promise then without defer
有一种方法可以在没有延迟的情况下调用这个 promise 吗?
我想用 bluebird
我希望它被称为具有很少超时的承诺链。
var step1 = function () {
console.log("This is step 1, args=", arguments);
return "ret 1";
};
var step2 = function () {
console.log("This is step 2, args=", arguments);
return "ret 2";
};
var deferred = Q.defer();
var promise0 = deferred.promise;
var promise1 = promise0.then(step1);
var promise2 = promise1.then(step2);
deferred.resolve("foo");
这是我使用的jsFiddle。
http://jsfiddle.net/HKhw8/1067/
更新
我需要在内部添加一些逻辑,但仍然向控制台显示步骤 1、2、3 我在这里做错了什么?
http://jsfiddle.net/HKhw8/1073/
Promise.resolve() // pass value you expect as argument in step1
.then(step1) // return value will be passed to step2 as argument
.then(step2);
蓝鸟具体答案。
Promise.try(step1).then(step2);
如果您只使用同步步骤 - 不要使用承诺。
有一种方法可以在没有延迟的情况下调用这个 promise 吗? 我想用 bluebird
我希望它被称为具有很少超时的承诺链。
var step1 = function () {
console.log("This is step 1, args=", arguments);
return "ret 1";
};
var step2 = function () {
console.log("This is step 2, args=", arguments);
return "ret 2";
};
var deferred = Q.defer();
var promise0 = deferred.promise;
var promise1 = promise0.then(step1);
var promise2 = promise1.then(step2);
deferred.resolve("foo");
这是我使用的jsFiddle。
http://jsfiddle.net/HKhw8/1067/
更新
我需要在内部添加一些逻辑,但仍然向控制台显示步骤 1、2、3 我在这里做错了什么? http://jsfiddle.net/HKhw8/1073/
Promise.resolve() // pass value you expect as argument in step1
.then(step1) // return value will be passed to step2 as argument
.then(step2);
蓝鸟具体答案。
Promise.try(step1).then(step2);
如果您只使用同步步骤 - 不要使用承诺。