12

I know that't very silly, but how to start a promise chain? I have, for example,

var p = new Promise(function(resolve,reject) {
  setTimeout(function() {
    return resolve("Hi from promise after timeout");
  },1000);
});

How to run it? It should be like that,

when(p)
.then(function(msg) {
  console.log(msg);
})
.catch(function(error) {
  console.error(error);
});

But when is not defined.

Kasheftin
  • 7,509
  • 11
  • 41
  • 68
  • 3
    Promises are not "run" or "started". They are simple values that represent the outcome of an already-running asynchronous operation (`setTimeout` in your case) which is started when the promise is created. – Bergi Jun 20 '16 at 16:28

1 Answers1

11

You just need to do:

p.then(function(msg) {
   console.log(msg);
})
.catch(function(error) {
  console.error(error);
});
albertosh
  • 2,416
  • 7
  • 25
  • 32