2016-06-20 43 views
2

我知道這不是很愚蠢,但如何啓動承諾鏈?我有,例如,如何履行承諾

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

如何運行它?它應該是這樣的,

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

但是when沒有定義。

+0

承諾不是 「運行」 或 「開始」。它們是代表已經運行的異步操作(在你的情況下是'setTimeout')的結果的簡單值,它是在創建promise時啓動的。 – Bergi

回答

5

你只需要做:

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

非常感謝!謝謝。 – Kasheftin