promise.all mongoose operates database - Stack Overflow
曾经蜡笔没有小新
曾经蜡笔没有小新 2017-06-15 09:22:14
0
2
872

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.

曾经蜡笔没有小新
曾经蜡笔没有小新

reply all(2)
为情所困

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

習慣沉默

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!