2

我想知道为什么 $.when 解决得太早了?只有当所有其他承诺都已解决时,我才需要它来解决。我错过了什么吗?

更新:http: //jsfiddle.net/7hdx5j6z/6/

  var promises = []

  localforage.iterate(function(value, key) {
    if ( key.indexOf('params_') === -1 ) {
      promises.push(localforage.removeItem(key))
      console.log(promises)
    }
  })

  $.when.apply($, promises).then(function() {
    console.log('all done!')
  })
4

1 回答 1

2

iterate itself returns a promise. You need to wait on that before the promises array is populated.

fiddle

var promises = []

var x = localforage.iterate(function (value, key) {
    if (key.indexOf('params_') === -1) {
        var promise = localforage.removeItem(key)
        promises.push(promise)
        console.log(promise)
    }
})

console.log("x", x);
x.then(function () {
    $.when.apply($, promises).then(function () {
        console.log('all done!')
    })
});
于 2015-01-06T13:04:40.503 回答