Whether it's an ES6 Promise or a bluebird Promise, Q Promise, etc. How do I test to see if a given object is a Promise? How a promise library decides If it has a .then function - that's the only standard promise libraries use. The Promises/A+ specification has a notion called then able which is basically "an object with a then method". Promises will and should assimilat
无论是ES6 Promise还是蓝鸟Promise,Q Promise等等。 我如何测试以查看给定对象是否为Promise? 承诺库如何决定 如果它有一个.then函数 - 这是唯一的标准许诺库使用。 Promises / A +规范有一个叫做then的概念,它基本上是“一个具有then方法的对象”。 承诺将会并且应该用当时的方法吸收任何东西。 你提到的所有承诺实现都是这样做的。 如果我们看一下规格: 2.3.3.3如果then是一个函数,则使用x来调用它,第一个参
I'm looking for a way to return a promise only if another promise fails . How can I do that? The promises that I have look like this ( of course I've remove non-related code ) : getUserByUsername = function(username) { return promise(function(resolve, reject) { user = userModel.getByUsername(username); if ( user ) { resolve(user); } else { reject("user not found");
我正在寻找一种方法, 只有在另一个承诺失败的情况下才能返回承诺 。 我怎样才能做到这一点? 我看起来像这样的承诺(当然,我已经删除了不相关的代码): getUserByUsername = function(username) { return promise(function(resolve, reject) { user = userModel.getByUsername(username); if ( user ) { resolve(user); } else { reject("user not found"); } }); }; getUserByEmail = function(e
This question already has an answer here: Aren't promises just callbacks? 7 answers You can sequence promises without the ever increasing nesting like this: app.post('/Billing', function(req, res) { cart.getBasket(req).then(function(basket) { return cart.updateBasket(req); }).then(function() { return cart.updateDefaultShipment(req); }).then(function(basket) {
这个问题在这里已经有了答案: 不承诺只是回调? 7个答案 您可以在不增加嵌套的情况下对承诺进行排序: app.post('/Billing', function(req, res) { cart.getBasket(req).then(function(basket) { return cart.updateBasket(req); }).then(function() { return cart.updateDefaultShipment(req); }).then(function(basket) { return cart.getBasketObject(basket); }).then(functi
This question already has an answer here: Aren't promises just callbacks? 7 answers 关于什么taskA() .then(taskB) .then(taskC) .then(function(){});
这个问题在这里已经有了答案: 不承诺只是回调? 7个答案 关于什么taskA() .then(taskB) .then(taskC) .then(function(){});
This question already has an answer here: Aren't promises just callbacks? 7 answers Promises are built on top of callbacks. The latter are more primitive, more general, and take more work when you need to do something complex. For your example, they do pretty much the same thing. However, let's say you want to have three things resolve simultaneously (imagine requesting three res
这个问题在这里已经有了答案: 不承诺只是回调? 7个答案 承诺建立在回调之上。 后者更原始,更一般,当你需要做一些复杂的事情时需要更多的工作。 举个例子,他们做的事情几乎是一样的。 但是,假设您想同时解决三件事情(想象一下同时需要AJAX提供三种资源),并在三件事完成时继续。 承诺是微不足道的,因为基本上没有什么变化。 但通过回调,你需要设置一些标志/计数器,并且自己识别成功和失败状态 - 更多的工作
This question already has an answer here: Aren't promises just callbacks? 7 answers Try whit this: function getPost(id) { return Post .findOne({id: id}) .then( post => { return post; }); } using Q module function getCommentsAndLinks(post) { return Q.all([ Comment.find({id: post.id}), Links.find({id: post.id}) ]) .done( results => { let comme
这个问题在这里已经有了答案: 不承诺只是回调? 7个答案 试试这个: function getPost(id) { return Post .findOne({id: id}) .then( post => { return post; }); } 使用Q模块 function getCommentsAndLinks(post) { return Q.all([ Comment.find({id: post.id}), Links.find({id: post.id}) ]) .done( results => { let comments = results[0]; let links = results[1
This question already has an answer here: Aren't promises just callbacks? 7 answers Promises provide a more succinct and clear way of representing sequential asynchronous operations in javascript. They are effectively a different syntax for achieving the same effect as callbacks. The advantage is increased readability. Something like this aAsync() .then(bAsync) .then(cAsync) .d
这个问题在这里已经有了答案: 不承诺只是回调? 7个答案 Promise提供了一种更简洁明了的方式来表示JavaScript中的顺序异步操作。 它们实际上是一种用于实现与回调相同效果的不同语法。 优点是提高了可读性。 像这样的东西 aAsync() .then(bAsync) .then(cAsync) .done(finish); 更具可读性,相当于将每个单独的函数作为回调函数传递,比如 Async(function(){ return bAsync(function(){ return cAs
I know this has been asked several ocassions, but I think that none of the Q&A in SO refers to the scenario explained in this question. I've got a JavaScript component (it's a KO component, bu it doesn't matter). Depending on some actions in this component it can load asynchronously several different versions of a child component. This can happen several times. Ie originally
我知道这已经被问了几次,但我认为SO中的问答都没有涉及这个问题中解释的情况。 我有一个JavaScript组件(它是一个KO组件,无所谓)。 根据此组件中的某些操作,它可以异步加载子组件的几个不同版本。 这可能发生多次。 也就是说,它最初可以加载子A,然后将其替换为子B等等。 主要组件需要运行子组件的一些功能。 为此,它将一个名为registerApi的函数传递给子组件构造函数。 这样,当子组件完成加载时,它调用registe
I am somewhat puzzled about reasoning in difference in implementation of ES6 Promises and PEP3148 Futures. In Javascript, when Promise is resolved with another Promise, "outer" promise inherits the value of "inner" promise once it's resolved or rejected. In Python, "outer" future is instead immediately resolved with "inner" future itself, not with it&
我对ES6 Promises和PEP3148期货执行差异的推理有点困惑。 在Javascript中,当Promise用另一个Promise解决时,“外部”承诺在解决或拒绝后继承“内部”承诺的价值。 在Python中,“外部”未来将立即用“内在”未来本身解决,而不是最终的价值,这就是问题所在。 为了说明这一点,我为这两个平台提供了两个代码片段。 在Python中,代码如下所示: import asyncio async def foo(): return asyncio.sleep(delay=2, result=42) as
What are the differences between Deferreds, Promises and Futures? Is there a generally approved theory behind all these three? In light of apparent dislike for how I've attempted to answer the OP's question. The literal answer is, a promise is something shared w/ other objects, while a deferred should be kept private. Primarily, a deferred (which generally extends Promise) can resol
递延,承诺和期货之间有什么区别? 这三者背后有一个普遍认可的理论吗? 鉴于我显然不喜欢我试图回答OP的问题。 字面答案是,承诺是与其他对象共享的东西,而延迟应该保持私密。 首先,延期(通常延伸Promise)可以自行解决,而承诺可能无法做到。 如果你对细节感兴趣,那么检查Promises / A +。 就我所知,总体目标是通过标准化界面来提高清晰度并减少耦合。 请参阅@ jfriend00提供的阅读材料: 使用promise可以导