2013-12-15 4 views
0

Winjs 약속을 반환하는 데 문제가 있습니다. 코드에 어떤 문제가 있는지 전혀 알지 못합니다. 약속을하고 .done을하거나 내 약속이 아무것도하지 않을 때.약속 배열에 WinJS가 반환되었습니다.

코드 :

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++); 
      }); 

내가 어떤 제안을 매우 gratefull 될 것입니다.

+1

음 :

난 당신이 더 같은 코드를 작성하려는 생각합니다. 'new WinJS.Promise'의 함수 매개 변수는 전통적으로'c','e' 및'p'라는 세 개의 매개 변수를 취합니다. 결과를 생성하면'c (result)'를 호출합니다. –

답변

2

즉시 반환하고 싶지 않고 대신 요소가 가득 찬 배열을 반환하려고한다고 가정합니다. 당신이 약속을 만드는 방법이 아니에요,

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; 
     }); 
} 
+0

감사합니다 :) 때로는 아주 분명하지 않습니다;) 감사합니다! – pkruk

관련 문제