上一次回调中逐个删除记录的Javascript代码

上一次回调中逐个删除记录的Javascript代码,javascript,callback,promise,Javascript,Callback,Promise,如何从集合中删除所有记录?我试着如下所示,但是对于1000条记录,有1000次回调。应该有更好的办法 dpd.mycollection.get(function(result,error){ result.forEach(function(entry) { dpd.mycollection.del(entry.id,function(){}); }); }) 我只想在删除最后一条记录后执行一些代码。我只是不知道怎么做。假设: 目前还不清楚这段代码在哪里运行,但我建议使用 在没有任何库的

如何从集合中删除所有记录?我试着如下所示,但是对于1000条记录,有1000次回调。应该有更好的办法

dpd.mycollection.get(function(result,error){
result.forEach(function(entry) {
    dpd.mycollection.del(entry.id,function(){});
});
})
我只想在删除最后一条记录后执行一些代码。我只是不知道怎么做。

假设:


目前还不清楚这段代码在哪里运行,但我建议使用

在没有任何库的情况下执行此操作的方法是使用计数器:

var deleted_records = 0;

entries.forEach(function (entry) {
  dpd.mycollection.del(entry.id, function () {
    deleted_records++;

    //are more records pending to be deleted?
    if (deleted_records < entries.length) return;

    //at this point deleted_records is equal to the amount of entries
    //so we can asume all records have been deleted
    console.log('finished!');
  });
});
var已删除_记录=0;
entries.forEach(函数(entry){
dpd.mycollection.del(entry.id,函数(){
已删除的_记录++;
//是否有更多待删除的记录?
if(已删除的_记录
这是哪个库(如果有的话)?承诺在哪里?或者你想使用承诺,但还没有?在这种情况下,谷歌搜索“JavaScript承诺”,你会发现很多库。
function delete_entry (entry, callback) {
  dpd.mycollection.del(entry.id, callback);
}

async.each(entries, delete_entry, function (err) {
  console.log('finished!');
});
var deleted_records = 0;

entries.forEach(function (entry) {
  dpd.mycollection.del(entry.id, function () {
    deleted_records++;

    //are more records pending to be deleted?
    if (deleted_records < entries.length) return;

    //at this point deleted_records is equal to the amount of entries
    //so we can asume all records have been deleted
    console.log('finished!');
  });
});