promise数组中的WinJS返回

WinJS return in promise array

本文关键字:WinJS 返回 数组 promise      更新时间:2023-09-26

我在Winjs promise中返回数组时遇到问题,我不知道我的代码出了什么问题。当我创造了一个承诺并做到了。完成了或。那么我的承诺就什么都没有了。

代码:

function getSth(array) {
    return new WinJS.Promise(function () {
        var dbPath = Windows.Storage.ApplicationData.current.localFolder.path + '''_db.sqlite';
        var i = 0;
        SQLite3JS.openAsync(dbPath)
              .then(function (db) {
                  console.log('DB opened');
                  return db.eachAsync('SELECT * FROM sthh;', function (row) {
                      array[i++] = row.sth;
                      console.log('Get a ' + row.sth);
                  });
              })
             .then(function (db) {
                 console.log('close the db');
                 db.close();
             }).then(function () {
                 return array;
             });
        return array;
    })
}

在另一个文件中,我只是做了一些类似的事情:

    var array = [];
            var z = getSth(array).then(function () {
                console.log("AAA");
for (var i = 0; i < array.length; console.log("#" + array[i]), i++);
            });

如有任何建议,我将不胜感激。

我假设您不想立即返回,而是想在数组充满元素后返回数组?

我想你想写的代码更像这样:

function getSth(array) {
    var dbPath = Windows.Storage.ApplicationData.current.localFolder.path + '''_db.sqlite';
    var i = 0;
    return SQLite3JS.openAsync(dbPath)
          .then(function (db) {
              console.log('DB opened');
              return db.eachAsync('SELECT * FROM sthh;', function (row) {
                  array[i++] = row.sth;
                  console.log('Get a ' + row.sth);
              });
          })
         .then(function (db) {
             console.log('close the db');
             db.close();
         }).then(function () {
             return array;
         });
}