2014-12-19 83 views
0

如果我有這樣的事情如何構建一個返回一個常量字符串的藍鳥承諾?

return this.retrieveArticles(blogId).then(function(response){ 
    return response.articles; 
    }).then(_).call("findWhere", match).then(function(article){ 
    return { 
     "article": article 
    } 
    }); 

,我決定要砍上咬下

return response.articles; 
    }).then(_).call("findWhere", match).then(function(article){ 
    return { 
     "article": article 
    } 
    }); 

我怎麼做這樣的事情

Promise.return(articles).then(_).call("findWhere", match).then(function(article){ 
    return { 
     "article": article 
    } 
    }); 
+0

你的意思'Promise.resolve(_(文章).findWhere(比賽))'? – Bergi 2014-12-19 13:04:24

回答

2
Promise.resolve(_(articles)).call("findWhere", match); 

Promise.resolve(articles).then(_).call("findWhere", match); 
0
function promiseString(str){ 
    return new Promise(function(resolve, reject) { 
    return resolve(str) 
    }) 
} 

promiseString("hello world").then(function(x){ 
    console.log(x) 
} 
+0

這是一種cru。。 – 2014-12-19 12:32:19

1

then你可以直接返回值:

var p = someFn().then(function(){ 
    return 43; 
}); 
p.then(function(val){ 
    console.log(val); // 42 
}); 

如果在鏈不是你可以使用Promise.resolve

var p = Promise.resolve(42); 

p.then(function(val){ 
    console.log(val); // 42 
}); 

大多數圖書館提供的這些變體。另一個例子是藍鳥 - 在藍鳥中,你可以使用Promise.method強制函數返回一個承諾,無論你是否自己返回一個值或一個承諾。