0

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/

2 Answers2

0
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

Andrei Lesnitsky
  • 1,038
  • 7
  • 14
  • Thanks Andrei , In case I need to add new logic inside every step(every then like timout) how you should do it? –  Dec 27 '15 at 12:45
  • you can simply update function body, but in case you don't want do this you can add additional steps between `.then(step1)`, `then(step2)`. I guess you're using promises as some kind of control flow? If there is no _asynchronous_ stuff inside your steps, there is no need for pormises – Andrei Lesnitsky Dec 27 '15 at 12:53
  • Thanks I use async control flow and this is just example to understand this better, when I use your suggestion I dont see the console output for steps what am I doing wrong http://jsfiddle.net/HKhw8/1071/ –  Dec 27 '15 at 12:58
  • When promise resolved, callbacks passed to then methods called. I can see test1 and test2 in console, which is output of your anonymous functions you've passed to then-s – Andrei Lesnitsky Dec 27 '15 at 13:01
  • Thanks but you dont see the stes –  Dec 27 '15 at 13:03
  • Quick note - check out this new js features to work with async stuff: [co](https://www.npmjs.com/package/co), [async/await](https://github.com/tc39/ecmascript-asyncawait) – Andrei Lesnitsky Dec 27 '15 at 13:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/99046/discussion-between-andrei-volchenko-and-mark). – Andrei Lesnitsky Dec 27 '15 at 13:04
0

Bluebird specific answer.

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

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

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • HI Benjamin , Thanks but if I want to add addtional logic for example delay between the then and still see the steps console how would you do it ? –  Dec 27 '15 at 14:28
  • @Mark promises are very simple - they only work by `return` value - if you `return` a promise from a `then` it will wait for it before executing the next `then`. – Benjamin Gruenbaum Dec 27 '15 at 14:51
  • Thanks can you please provide example how should I make it work http://jsfiddle.net/HKhw8/1073/ –  Dec 27 '15 at 14:58
  • @Mark no, you learn - read stackoverflow.com/questions/22519784/how-do-i-convert-an-existing-callback-api-to-promises/ to understand how to create a version of `setTimeout` that returns promise. Then read http://stackoverflow.com/questions/22539815/arent-promises-just-callbacks to understand the chaining aspect. _after_ you've read both and wrote the code if it doesn't work I'll gladly help you with that :) – Benjamin Gruenbaum Dec 27 '15 at 15:02