简体   繁体   English

你能等待一个 function 未定义为异步但在其中声明新的 Promise 吗?

[英]can you await a function not defined as async but declared new Promise inside of it?

We have this in our code:我们的代码中有这个:

const sqlUpsertRecommendations = recommendationsArray => {
// eslint-disable-next-line no-new

  new Promise(resolve => {
    const insertStatus = {
      ....
    };
    if (!recommendationsArray.length > 0) {
      resolve({
        error: false,
        results: insertStatus,
        localMessage: 'recommendationsArray.length = 0',
      });
    }

    insertStatus.arrayEmpty = false;

    dbConfig.transaction(txn => {
      for (let i = 0; i < recommendationsArray.length; i++) {
        const {
          id,
          recommendation_attribute,
          recommendation_value,
          segment_id,
          target_attribute,
          target_value,
          is_deleted,
          updated_at,
          name,
          image,
          border_color,
        } = recommendationsArray[i];

        const params = [
          id,
          recommendation_attribute,
          recommendation_value,
          segment_id,
          target_attribute,
          target_value,
          is_deleted,
          updated_at,
          name,
          image,
          border_color,
        ];

        try {
          txn.executeSql(
            upsertRecommendations,
            params,
            (_tx, _results) => {
              resolve({ error: false, results: insertStatus });
            },

            error => {
              crashlytics().recordError(error);
              if (__DEV__) {
                console.log('sqlUpsertRecommendations error:', error);
              }
              const { notInserted } = insertStatus;
              insertStatus.inserted = notInserted + 1;

              if (1 + i === recommendationsArray.length) {
                resolve({ error: false, ...error, results: insertStatus });
              }
            },
          );
        } catch (e) {
          crashlytics().recordError(e);
        }
      }
    });
  });
};

I am learning async await and people in our team are using it like我正在学习异步等待,我们团队中的人正在使用它

const testFunction = async () => { 
....
await sqlUpsertRecommendations(parsedData)

But vscode is saying但是vscode在说

'await' has no effect on the type of this expression. 'await' 对这个表达式的类型没有影响。

I just need idea how to best approach and use this kind of code?我只需要知道如何最好地处理和使用这种代码?

You need to actually return the new Promise , then you can await it.您需要实际return new Promise ,然后您可以await它。 If you don't, you just start a Promise and then immediately return undefined , and you can't await undefined .如果你不这样做,你只需启动一个 Promise 然后立即返回undefined ,你不能await undefined

const sqlUpsertRecommendations = recommendationsArray => {
// eslint-disable-next-line no-new

  return new Promise(resolve => {
    // ... rest of code

async is really just syntactic sugar for creating and using Promise s. async实际上只是用于创建和使用Promise的语法糖。 An async function just returns a new Promise and that's what await cares about, so await will work if you have a normal function that returns a Promise . An async function just returns a new Promise and that's what await cares about, so await will work if you have a normal function that returns a Promise .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM