React Native 中的 Promise

Promise on Reactnative

我写了一个在sqlite中插入数据的承诺:

var promise_insertData=new Promise(function(resolve, reject){
          db.transaction((tx) =>{
                tx.executeSql('INSERT INTO chontact (recordID,user, phone) VALUES (?,?,?)',[recordID, name, number],
                (tx, results) => {
                    var status=(results.rowsAffected > 0) ? true : false;
                    if(status)
                        resolve()
                    else
                        reject()
                    }
                );
              });
                        reject()
})

当我尝试插入数据时出现错误

promise_insertData(db,recordid,name,phone).then(function(result){
                Toast.show('ok...');
                del_item();
            }).catch(function(error){
                Toast.show('error');
              })

错误:

TypeError: (0, _$$_REQUIRE(_dependencyMap[12], "./JSfiles/functions").promise_insertData) is not a function. (In '(0, _$$_REQUIRE(_dependencyMap[12], "./JSfiles/functions").promise_insertData)(db, recordid, textinput, textinputphone)', '(0, _$$_REQUIRE(_dependencyMap[12], "./JSfiles/functions").promise_insertData)' is an instance of Promise)

试一试。您应该能够 promise_insertData(stuff).then(doSomethingElse) 我不确定您代码中的最后一个 reject(),这可能是错误的。

const promise_insertData = (db, recordid, name, phone) => {
  return new Promise(function(resolve, reject){
    db.transaction((tx) =>{
      tx.executeSql('INSERT INTO chontact (recordID,user,phone) VALUES (?,?,?)',[recordid, name, phone],
      (tx, results) => {
          var status=(results.rowsAffected > 0) ? true : false;
          if(status)
              resolve()
          else
              reject()
      });
    });
    reject() // ??
  })
};