什么方面的承诺库的不规范不覆盖?实施之间有什么不同?
请举例说明实际差异(例如Bluebird和Q之间).
# Ember : 1.4.0
# Ember Data : 1.0.0-beta.7+canary.b45e23ba
Run Code Online (Sandbox Code Playgroud)
我简化了我的用例,使问题更容易理解和更好.假设我们有3种型号:Country,Region和Area:
Country:
- id: DS.attr('number')
- name: DS.attr('string')
- regions: DS.hasMany('region')
Region:
- id: DS.attr('number')
- name: DS.attr('string')
- country: DS.belongsTo('country')
- areas: DS.hasMany('area')
Area:
- id: DS.attr('number')
- name: DS.attr('string')
- region: DS.belongsTo('region')
Run Code Online (Sandbox Code Playgroud)
Route的模型钩子应该返回一个对象数组.像这样:
注意:缩进只是为了使示例更具可读性.
Country I
Region A
Area 1
Area 2
Region B
Area 3
Country II
Region C
Area 4
Country III
Region D
Area …Run Code Online (Sandbox Code Playgroud) 我坚持以下几点:
脚本返回任意数字n或数组,如下所示:
[["a"], ["b"], ["c"], ["d"]]
Run Code Online (Sandbox Code Playgroud)
我需要使用promise循环遍历数组then(),但由于我不知道会有多少元素,所以我最终做到了这一点:
var bundle_list = [["a"], ["b"], ["c"], ["d"]];
var x = bundle_list.reduce(function(current, next) {
console.log(current);
// requestBundle will also return a promise
return requestBundle(current)
.then(function(bundle_response) {
// do foo
console.log("CALLING NEXT")
console.log(next);
return RSVP.resolve(next);
});
})
x.then(function(last_response) {
return console.log("DONE")
});
Run Code Online (Sandbox Code Playgroud)
我的问题是我的reduce/map两个都在我的异步代码运行之前触发所有迭代,所以我得到3倍的current控制台,然后是done控制台.所以我的所有地图"循环"都会立即运行,结果会稍后(稍后)计时...
我正在使用这个RSVP实现,但它是A +所以不应该是一个问题.我一直在努力解决这里提供的答案,但我无法让它正常工作.
问题:
是否可以使用任意数量的then语句创建"then-chain" .如果是这样,一些指针表示赞赏!
谢谢!
我正在使用Ember的PromiseProxyMixin和AJAX数据调用以及Ember RSVP Promises.我不想在每个路由/模板中加入错误处理,而是将拒绝的承诺冒充到Application路由中的错误处理程序,如下所示:
export default Ember.Route.extend({
actions: {
error: function(error, transition) {
return this.transitionTo('error');
return false;
}
}
});
Run Code Online (Sandbox Code Playgroud)
目前,如果承诺被拒绝,被拒绝的承诺似乎没有出现在Application路由中(这是因为PromiseProxyMixin附加到promise的.fail()函数并阻止进一步冒泡?如果是这样,有没有办法继续冒泡?)
是否可以使用PromiseProxyMixin并允许被拒绝的承诺冒泡到Application路由?
我是承诺并使用rsvp实现的新手.
我想异步读取文件列表,然后只有在读取了所有文件后才进入另一个任务.
我已经有了基本结构来读取一个文件,并链接到下一个任务:
var loadFile = function (path) {
return new rsvp.Promise(function (resolve, reject) {
fs.readFile (path, 'utf8', function (error, data) {
if (error) {
reject(error);
}
resolve(data);
});
});
};
loadFile('src/index.txt').then(function (data) {
console.log(data);
return nextTask(data);
}).then(function (output) {
//do something with output
}).catch(function (error) {
console.log(error);
});
Run Code Online (Sandbox Code Playgroud)
我想做这样的事情:
loadFile(['src/index.txt', 'src/extra.txt', 'src/another.txt']).then( ...
Run Code Online (Sandbox Code Playgroud)
我在文档中看到过承诺和承诺的数组,但我不知道哪个是最相关的,或者如何使用它们.我需要在上面的问题的上下文中使用它们的一个例子来理解它们.
我有两个简单的方法:
function do(x,y){
if(x){
XHR1().success();
}
if(y){
XHR2().success();
}
}
function done(){
return something;
}
Run Code Online (Sandbox Code Playgroud)
现在我只想确保done()在do()完成时调用(**do() 方法包含对 Mysql DB 的异步请求)
我怎样才能做到这一点?**
显然,这不会按顺序排列这些方法:
do(x=1,y=1);
done(); //this can run also if do() has not finished yet
Run Code Online (Sandbox Code Playgroud)
所以我尝试:
function do(x,y,_callback){
if(x){
XHR1().success();
}
if(y){
XHR2().success();
}
_callback();
}
function done(){
return something;
}
do(x=1,y=1,done()); // this won't work the same cause callback is not dependent by XHR response
Run Code Online (Sandbox Code Playgroud)
这是我用于承诺的内容https://github.com/tildeio/rsvp.js/#arrays-of-promises
我正在为一个返回promise的函数编写测试,并且无法在解析或拒绝promise时运行断言.我不能在这个项目上使用ES6所以我使用的是声称实现Promises/A +规范的rsvp.js库.我正在使用最新的qunit版本1.17.1
被测功能:
var loadGroups = function(data) {
return new RSVP.Promise(function(resolve, reject) {
var groups = [];
// ... some code to populate the groups array
if(groups.length > 0) {
resolve(groups);
} else {
reject("No groups were loaded");
}
});
};
Run Code Online (Sandbox Code Playgroud)
成功测试:
test('success test', function(assert) {
assert.expect(1);
var promise = loadGroups(testData);
promise.then(function(groups) {
assert.deepEquals(groups.length, 1);
});
return promise;
});
Run Code Online (Sandbox Code Playgroud)
这失败了"预期的1个断言,但是0运行"
失败测试:
test('failure test', function(assert) {
assert.expect(1);
var promise = loadGroups([]);
promise.then(null, function(message) {
assert.equals(message, "No groups were loaded");
});
return …Run Code Online (Sandbox Code Playgroud) 我正在使用GitHub API来加载特定路径中的模型
我正在为我的个人github详细信息做两个承诺:https://api.github.com/users/user,另一个用于我的GitHub存储库https://api.github.com/users/user/repos
我可以单独加载模型,但问题是我没有想到如何在我的特定路线中同时加载两个模型.
看代码
var IndexRoute = Ember.Route.extend({
model: function(params) {
var url, self, git;
self = this;
git = this.store.createRecord('git',{});
url = 'https://api.github.com/users/user';
return new Ember.RSVP.Promise(function(resolve, reject) {
return Ember.$.getJSON(url, function(data) {
var item = [];
git.setProperties({
name: data.name,
login: data.login,
location: data.location,
company: data.company,
followers: data.followers,
following: data.following
});
item.pushObject(git);
return resolve(item);
});
});
},
model: function(params){
var self, url, repoListProxy;
self = this;
url = 'https://api.github.com/users/user/repos';
repoListProxy = Ember.ArrayProxy.create({
content: [] …Run Code Online (Sandbox Code Playgroud) 当我想在控制器操作中从服务器获取帐户时,例如:
account: false,
actions: {
// When the oAuth popup returns this method will be called
fetchUser: function(id)
{
// Get the account from the server
this.store.find('account', id).then(function(account)
{
this.set('account', account);
}, function(error)
{
console.log('error', error);
});
},
}
Run Code Online (Sandbox Code Playgroud)
它会抛出一个错误,因为该行上的"this" this.set('account', account)不再是控制器.如何在此承诺回调中设置控制器上的"帐户"?
rsvp-promise ×9
javascript ×8
promise ×7
ember.js ×4
asynchronous ×2
node.js ×2
rsvp.js ×2
bluebird ×1
ember-cli ×1
ember-data ×1
mapreduce ×1
q ×1
qunit ×1