2014-12-19 87 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强制函数返回一个承诺,无论你是否自己返回一个值或一个承诺。