How to run a promise

Kasheftin picture Kasheftin · Jun 20, 2016 · Viewed 7.3k times · Source

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.

Answer

albertosh picture albertosh · Jun 20, 2016

You just need to do:

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