2016-02-26 42 views
0

我試圖環繞的承諾設法解決方案,我發現自己在很多次我的頭:播種MongoDB數據庫。所以,我得到了下面的腳本,我想變成一個承諾,最好使用ES6,但會採取藍鳥作爲一個答案,如果是這樣的話。 這是我使用setTimeout給時間去寫操作關閉數據庫之前:打開一個貓鼬播種腳本到一個承諾

// Importing the connection to the `bookshelf` db. 
 
var bookshelfConn = require('./database'); 
 

 
// Importing the Mongoose model we'll use to write to the db. 
 
var Book = require('./models/Book'); 
 

 
// Importing the Data to populate the db. 
 
var books = require('./dataset'); 
 

 
// When the connection is ready, do the music! 
 
bookshelfConn.on('open', function() { 
 
    dropDb(seedDb, closeDb); 
 
}); 
 

 
function dropDb (cb1, cb2) { 
 
    bookshelfConn.db.dropDatabase(); 
 
    console.log('Database dropped!'); 
 
    cb1(cb2); 
 
} 
 

 
function seedDb (cb) { 
 
    console.time('Seeding Time'); // Benchmarking the seed process. 
 

 
    // Warning! Slow IO operation. 
 
    books.forEach(function (book) { 
 
    new Book(book).save(function (err) { 
 
     if (err) console.log('Oopsie!', err); 
 
    }); 
 
    console.log('Seeding:', book); 
 
    }); 
 

 
    console.timeEnd('Seeding Time'); // Benchmarking the seed process. 
 

 
    cb(); 
 
} 
 

 
function closeDb() { 
 
    setTimeout(function() { 
 
    bookshelfConn.close(function() { 
 
     console.log('Mongoose connection closed!'); 
 
    }); 
 
    }, 1000); // Delay closing connection to give enough time to seed! 
 
}

更新代碼,以反映zangw的回答是:

// Importing the connection to the `bookshelf` db. 
 
var bookshelfConn = require('./database'); 
 

 
// Importing the Mongoose model we'll use to write to the db. 
 
var Book = require('./models/Book'); 
 

 
// Importing the Data to populate the db. 
 
var books = require('./dataset'); 
 

 
// When the connection is ready, do the music! 
 
bookshelfConn.on('open', function() { 
 
    // Here we'll keep an array of Promises 
 
    var booksOps = []; 
 

 
    // We drop the db as soon the connection is open 
 
    bookshelfConn.db.dropDatabase(function() { 
 
    console.log('Database dropped'); 
 
    }); 
 

 
    // Creating a Promise for each save operation 
 
    books.forEach(function (book) { 
 
    booksOps.push(saveBookAsync(book)); 
 
    }); 
 

 
    // Running all the promises sequentially, and THEN 
 
    // closing the database. 
 
    Promise.all(booksOps).then(function() { 
 
    bookshelfConn.close(function() { 
 
     console.log('Mongoose connection closed!'); 
 
    }); 
 
    }); 
 

 
    // This function returns a Promise. 
 
    function saveBookAsync (book) { 
 
    return new Promise(function (resolve, reject) { 
 
     new Book(book).save(function (err) { 
 
     if (err) reject(err); 
 
     else resolve(); 
 
     }); 
 
    }); 
 
    } 
 
});

回答

1

請嘗試通過new PromisePromise.all

new Promise創建一個新的承諾。傳入的函數將接收函數resolve和reject作爲其參數,可以調用它來封鎖創建的promise的命運。

Promise.all是當你想等待一個以上承諾完成有用的。

var bookOps = []; 

books.forEach(function (book) { 
    bookOps.push(saveBookAsync(book)); 
}); 

Promise.all(bookOps).then(function() { 
    bookshelfConn.close(function() { 
     console.log('Mongoose connection closed!'); 
    }); 
}); 

function saveBookAsync(book) { 
    return new Promise(function(resolve, reject) { 
     new Book(book).save(function(err) { 
      if (err) 
       reject(err); 
      else 
       resolve(); 
     }) 
    }); 
} 
+0

得益於它完美的作品。我根本不知道'Promise.all')) –