简体   繁体   English

承诺环回模型

[英]Promisfy loopback models

In unit testing Loopback it is necessary to use callbacks with the upsert methods.在单元测试 Loopback 中,有必要使用带有 upsert 方法的回调。 So for example ... instead of writing something along the lines of this:因此,例如......而不是沿着这个方向写一些东西:

before(function () {

    Student = server.models.Student
    Course = server.models.Course


    Course.upsert({id: 1, key: 'A', department: 'Original department'})
    Student.upsert({id: 1, points: 5000})


})

it is necessary to ensure that callbacks are used with the upsert.有必要确保回调与 upsert 一起使用。 Since I have a lot of models I am initializing below I use async:由于我有很多模型我在下面初始化我使用异步:

before(function (done) {

    Student = server.models.Student
    Course = server.models.Course


    async.waterfall([
            function (callback) {
                Course.upsert({id: 1, key: 'A', department: 'Original department'}, callback)
            },
            function (f, callback) {
                Student.upsert({id: 1, points: 5000}, callback)
            },

        ],
        function (err, results) {
            done(err)
        })

})

Instead of async, how would the code above be changed to use Promises?上面的代码将如何更改为使用 Promises,而不是 async?

My thought is that with promises, I would be able to write some code that looks like this:我的想法是,有了 Promise,我就可以编写一些如下所示的代码:

before(function (done) {

    Student = server.models.Student
    Course = server.models.Course


    Course.upsert({id: 1, key: 'A', department: 'Original department'})
      .then(Student.upsert({id: 1, points: 5000})
      .then(function(err) { done(err) } 


})

but I have been unsuccessful tying into promises.但我一直没有成功地兑现承诺。

EDIT from answers below ...从下面的答案编辑...

 before(function (done) {


    Course = server.models.Course

    Course.upsertWithPromise = Promise.promisify(Course.upsert)

    Course.upsertWithPromise({id: 1, key: 'A', department: 'Original department'})
        .then(done)

  } 




it.only('Course upsert', function (done) {

    Course.findById(1, function (err, course) {

        expect(course.id).to.equal(1)
        expect(course.department).to.equal('Original department')

        done()


    })
})

There are two possible solutions.有两种可能的解决方案。 First is manual promisification.首先是手动承诺。 Your functions will look like this:您的函数将如下所示:

server.models.Student.upsert = function(data) {  // Note NO CALLBACK
  return new Promise(function(resolve, reject) {
    // Here can do all async staff and when done sucessfully call:
    resolve(result);
    // OR on error call:
    reject(err);
  });
}

Second solution will be use library for the same purpose.第二种解决方案是将库用于相同目的。

  1. var q = require('q'); var q = require('q'); // Use q library // 使用q库
  2. DO NOT modify your models.不要修改你的模型。 They must take callback as last argument (follow node convention)他们必须将回调作为最后一个参数(遵循节点约定)
  3. var server.models.Student.upsertWithPromice = q.denodeify(server.models.Student.upsert); var server.models.Student.upsertWithPromice = q.denodeify(server.models.Student.upsert);
  4. profit.利润。

Then your code from your example should work fine.那么您的示例中的代码应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM