zoukankan      html  css  js  c++  java
  • mongoose的promise(转发)

    Published on July 28, 2015

    mongo node mongoose promises

    Working with promises and mongoose just became a whole lot easier with the 4.1 release, which added the ability to specify alternative promise libraries. Prior to that, promise support was limited to the mpromise way of using promises. For some folks, including myself, this meant there wasn't a friendly .catch method available to the promise chain. In this post, I'll quickly cover how to switch over to other supported promise libraries and show how using promises can clean up your mongoose callbacks.

    Normally, when using mongoose, you just need to require it. In order to switch out the promise provider, you'll also need to require the promise library and pointmongoose.Promise to it. In the following example, I set the promise library to bluebird:

    var mongoose = require('mongoose');
    // set Promise provider to bluebird
    mongoose.Promise = require('bluebird');
    

    Here's the example for using native promises or q:

    // q
    mongoose.Promise = require('q').Promise;
    // native promises
    mongoose.Promise = global.Promise;
    

    That's as simple and non-hacky as one could hope for. Next up, I'll show what typical mongoose callbacks look like and how you can swap those out for promises. In these last examples, I'll look up a user by id, update the user's name, and save it:

    // error first callback style
    User.findById('123', function(err, user) {
      if (err) {
        return console.log('error:', err);
      }
    
      user.name = 'Robert Paulson';
    
      user.save(function(err) {
        // yet another err object to deal with
        if (err) {
          return console.log('error:', err);
        }
        console.log('updated user: ' + user.name);
        // do something with updated user
      });
    });
    

    The above callback example shows the first level of nesting and multiple error handlers. That's not too bad, but with more logic it can easily become visually overwhelming. In the last example, I'll show what the same task looks like using promises. We'll switch to using Model queries that return a promise via the .exec() function.

    var promise = User.findById('123').exec();
    
    promise.then(function(user) {
      user.name = 'Robert Paulson';
    
      return user.save(); // returns a promise
    })
    .then(function(user) {
      console.log('updated user: ' + user.name);
      // do something with updated user
    })
    .catch(function(err){
      // just need one of these
      console.log('error:', err);
    });
    

    Note that there was only one error handler for both of the promises,findById(id).exec() and user.save(). For me, the benefit of using promises is really in the ability to read what's going on in the code and to consolidate error handling into one place with the option to break that out if needed. If that interests you, give promises in mongoose a try

  • 相关阅读:
    解决ffmpeg打开流各种超时问题
    ffmpeg函数使用
    如何从AVFrame::data【0】里获取RGB24数据和YUYV422数据
    ffmpeg取rtsp流时av_read_frame阻塞的解决办法
    FFMPEG实时解码网络视频流(回调方式)
    JavaScript 演练(7). 赋值与引用
    JavaScript 演练(5). 模拟类
    曾经对 TMemoryStream.Memory 错误的理解
    JavaScript 演练(10). 谁的 this ?
    JavaScript 演练(6). 函数的定义与自执行
  • 原文地址:https://www.cnblogs.com/jay--zhang/p/5911667.html
Copyright © 2011-2022 走看看