search

Home  >  Q&A  >  body text

promise.all mongoose operates database - Stack Overflow

Use Promise.all([p1, p2]) to operate two promise functions. Both functions use mongoose internally to operate the database.

If p1 is rejected and p2 is resolved.

My question is, p2 has actually performed operations on the database. Is that so? Or does promise.all ensure that both p1 and p2 resolve before performing actual database operations?

It feels like it is a mongoose transactional issue.

曾经蜡笔没有小新曾经蜡笔没有小新2719 days ago899

reply all(2)I'll reply

  • 为情所困

    为情所困2017-06-15 09:24:14

    Promise.all resolves when all given iterables complete, or rejects when any promises fail.
    If any promises are passed that rejects , all Promise values ​​immediately fail, discarding all other promises if they are unresolved. If passed any empty array, this method will complete immediately.

    var p0 = new Promise((resolve, reject) => {
          setTimeout(() => {
              console.log(1)
          }, 100);
    }); 
    var p1 = Promise.resolve(3);
    var p2 = 1337;
    var p3 = new Promise((resolve, reject) => {
      setTimeout(reject, 100, "foo");
    }); 
    var p4 = new Promise((resolve, reject) => {
          setTimeout(() => {
              console.log(2)
          }, 100);
    });
    var p = Promise.all([p0, p1, p2, p3, p4]).then(values => { 
      console.log(values);
    }, values => {
        console.log(values) // foo
    }); // 1 2

    1) Every promise in promise all will be executed

    2) At this time, the status of p becomes rejected

    reply
    0
  • 習慣沉默

    習慣沉默2017-06-15 09:24:14

    For your situation, I happened to write a package, https://github.com/zaaack/mon... It should be able to meet your needs

    reply
    0
  • Cancelreply