用承诺将水线电话链接起来 [英] Chaining waterline calls with Promises

查看:99
本文介绍了用承诺将水线电话链接起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去3天,我一直都在撞墙.

我正在使用sailsjs&捆绑的水线ORM.我想一个接一个地运行数据库调用.我知道我可以通过嵌套"then"调用来做到这一点,但这看起来是错误的.

我已经看过几次Q文档和教程,但是我仍然不知道如何依次连接和触发现有Promises中的然后"调用:(

我要:

  1. 创建用户
  2. 创建动作
  3. 链接用户&行动
  4. 更新用户
  5. 更新操作

我的代码看起来像

 var mail = 'test@test.com';

 Users.create({email:mail, name:''}).then(console.log).fail(console.log);

 Actions.create({actionID:123})
 .then(function(error, action){
        Users.findOneByEmail(mail).then(function(person){
            person.actions.add(action.id);
            person.save(console.log);
        }).fail(console.log)  
     });

 Users.update({email:mail},{name:'Brian'}).exec(console.log);
 Actions.update({actionID:123},{now:'running'}).exec(console.log);

从代码中可以看到,我一直在使用exec&然后:P

我认为方法是连接 Users.create(...).then-> Action.create(...).then-> Users.findOneByEmail(...).then-> *和更新.

非常感谢任何帮助

解决方案

因此,经过一天的研究.我想我已经破解了.

注意::我制作的第一个版本通过返回创建内容来排着长队"(删除了厄运金字塔 ).这使我可以在下一行调用,然后触发创建. http://documentup.com/kriskowal/q/#tutorial/chaining

这是我的最终版本

var mail = 'test@test.com';    

Users.Create({email:mail,name:''})
  .then(function(user){
    return [Actions.create({actionID:123}),user];
  }).spread(function(action, user){

     user.action.add(action.id);
     user.name = 'Brian';
     user.save();

     action.now = 'running';
     action.save();
  }).catch(console.error);

其中一项很酷的事情是传播",它使您可以排列承诺"和值",以将它们都已完成的返回到下一个然后".

I have been hitting my head off a wall on this for the last 3 days.

I am using sailsjs & the waterline ORM that comes bundled. I want to run DB calls one after an other. I know I can do this by nesting inside "then" calls but it just looks wrong.

I have gone over the Q documentation and tutorials several times but I still don't get how to connect and fire "then" calls from existing Promises sequentially :(

I want to:

  1. create a user
  2. create a action
  3. link the user & action
  4. update the user
  5. update the action

My code looks like

 var mail = 'test@test.com';

 Users.create({email:mail, name:''}).then(console.log).fail(console.log);

 Actions.create({actionID:123})
 .then(function(error, action){
        Users.findOneByEmail(mail).then(function(person){
            person.actions.add(action.id);
            person.save(console.log);
        }).fail(console.log)  
     });

 Users.update({email:mail},{name:'Brian'}).exec(console.log);
 Actions.update({actionID:123},{now:'running'}).exec(console.log);

As you can see from the code I've been using a mix of exec & then :P

I think the way is to connect the Users.create(...).then -> Action.create(...).then -> Users.findOneByEmail(...).then -> *and the updates.

Huge thanks from any help

解决方案

So after a day's research. I think I've cracked it.

Note: The first version I got working had the "then"s lined-up(removing the pyramid of doom) by returning the create. This allow me to call then on the next line to fire the create. http://documentup.com/kriskowal/q/#tutorial/chaining

Here's my final version

var mail = 'test@test.com';    

Users.Create({email:mail,name:''})
  .then(function(user){
    return [Actions.create({actionID:123}),user];
  }).spread(function(action, user){

     user.action.add(action.id);
     user.name = 'Brian';
     user.save();

     action.now = 'running';
     action.save();
  }).catch(console.error);

One of the cool things is the "spread" that allows you to line-up "Promises" and "values" to be return one they all have completed into the next "then".

这篇关于用承诺将水线电话链接起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆