简体   繁体   中英

Call to promise then without defer

There is a way to call to this promises without the defer ? I want to use bluebird

I want it to be called as promise chain with little timeout.

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");

This is the jsFiddle which I use.

http://jsfiddle.net/HKhw8/1067/

Update

I need to add inside every then some logic but still display to the console the step 1,2,3 what am I doing wrong here? 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.resolve docs

Bluebird specific answer.

Promise.try(step1).then(step2);

If you're using synchronous steps only - don't use promises.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM