【发布时间】:2013-12-15 13:32:44
【问题描述】:
我在 Winjs 承诺中返回数组时遇到问题,我不知道我的代码有什么问题。当我创建一个承诺并执行 .done 或 .then 时,我的承诺什么也不做。
代码:
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++);
});
如有任何建议,我将不胜感激。
【问题讨论】:
-
嗯,这不是你创建承诺的方式。
new WinJS.Promise的函数参数采用三个参数,传统上命名为c、e和p。产生结果后调用c(result)。
标签: javascript windows-8 microsoft-metro winjs