Tag: 答应

在Promise.then函数中不会更改外部variables

在使用thinky.js的节点上,我试图遍历一个循环,并将每个项目添加到一个数组。 但是,这个由于某种原因是行不通的。 在另一个地方,它是有效的,只是没有Promise.thenfunction。 为什么这不起作用? var fixedItems = []; for (i in tradeItems) { var item = tradeItems[i]; Item.get(item["id"]).run().then(function(result) { var f = { "assetid": result["asset_id"] }; console.log(f); // WOrks fixedItems.push(f); // Doesn't work }); } console.log(fixedItems); // Nothing

将对象绑定到Promise.then()参数的正确方法

我发现了一个困难的方法,那就是不能简单地将对象的函数传递给蓝鸟。 我假设蓝鸟then正在做一些魔术,并在匿名函数中包装传递函数。 所以我附加了一个.bind的function,它的工作。 这是与蓝鸟做到这一点的正确方法? 还是有更好的办法? var Promise = require("bluebird") var Chair = function(){ this.color = "red" return this } Chair.prototype.build = function(wood){ return this.color + " " + wood } var chair = new Chair() //var x = chair.build("cherry") Promise.resolve("cherry") .then(chair.build.bind(chair)) // color is undefined without bind .then(console.log) 我知道这一切都不是asynchronous,所以请同步示例,我的用法是asynchronous。