javascript - WinJS 在 Promise 数组中返回

标签 javascript windows-8 microsoft-metro winjs

我在 Winjs Promise 中返回数组时遇到问题,我不知道我的代码出了什么问题。当我创建一个 promise 并执行 .done 或 .then 时,我的 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;
         });
}

关于javascript - WinJS 在 Promise 数组中返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20595102/

相关文章:

python - 在 CMD 中运行 Python 不起作用

android - 用于表示多模式用户界面的状态图建模的可视化编程工具

javascript - 范围在 1-999999 之间且精度高达小数点后 2 位的正则表达式

javascript - js 代码在 Rails 中不起作用

postgresql - 如何在 Ubuntu 中找到 pgAdminIII GUI

javascript - 在WinJS.UI.ListView中设置滚动位置

html - Windows 8 Metro 应用程序中的弹出控件?

Javascript 拒绝工作

javascript - 多语言应用程序中的 Angular JS 路由不起作用

javascript - 如何在 Windows 8 对话框中包含复选框?