【问题标题】:TypeScript and Promise wrapper with Async/Await. How do I tie in legacy code?带有 Async/Await 的 TypeScript 和 Promise 包装器。如何绑定遗留代码?
【发布时间】:2017-06-19 18:44:09
【问题描述】:

我正在尝试创建一个包装承诺的 JavaScript/typescript 函数,方法是返回一个新的 Promise,添加一个 try catch 和一个回调以供用户编写代码。这是概念:

function XPromise(code) {
    return new Promise((resolve, reject) => {
        try {
           resolve(code());
        } catch (exception) {
            reject(exception);
        }
    });
}

但是我将如何在这样的情况下使用上面的 sn-p:

async function GetData(testClient, project, testPlan, suiteId) {
    return XPromise(code => {
        console.debug("GetData");
        testClient.getData(project, testPlan, suiteId)
            .then(data => {
                if (data.length === 0) reject(data);
                resolve(data);
            });
}

遗留代码使用 .then 构造,这是放置拒绝和解析的理想位置,但不存在执行此操作的函数。

如果我这样做:

function XPromise(code, resolve, reject) {
    return new Promise((resolve, reject) => {
        try {
            resolve(code());
        } catch (exception) {
            reject(exception);
        }
    });
}

async function GetData(testClient, project, testPlan, suiteId) {
    return XPromise(code => {
        console.debug("GetData");
        testClient.getData(project, testPlan, suiteId)
            .then(data => {
                if (data.length === 0) reject(data);
                resolve(data);
            });
    },
    resolve => { },
    reject => { }
}

我不知道如何让 then 逻辑在“低级”函数中执行它。

我想要新的 Promise 包装器的原因是我有大量的这些东西要实现......我不想在整个代码中这样做。这段代码可以编译,但需要我为我实现的每个函数编写 New Promises 和 Try Catch 语句。

async function GetData(testClient, project, testPlan, suiteId) {
    return new Promise((resolve, reject) => {
        console.debug("GetPoints");
        try {
            testClient.getData(project, testPlan, suiteId)
                .then(data => {
                    if (data.length === 0) reject(data);
                    resolve(data);
                });
        } catch (exception) { reject(exception); }
    });
}

【问题讨论】:

    标签: javascript asynchronous typescript async-await


    【解决方案1】:

    你不应该传递值参数解析,拒绝新的 Promise()。它们是在调用你的回调时由本机承诺对象给出的。您只需调用它来使该承诺解决或拒绝。

    new Promise((resolve,reject)=>{
      if(condition){ 
       resolve();// resolve is callback sent by promise object. you just invoke it
      }else{
       reject(); like resolve reject makes this promise to fail
      }
    });
    

    我认为你最后提到的逻辑是正确的

     async function GetData(testClient, project, testPlan, suiteId) {
        return new Promise((resolve, reject) => {
            console.debug("GetPoints");
            try {
                testClient.getData(project, testPlan, suiteId)
                    .then(data => {
                        if (data.length === 0) reject(data);
                        resolve(data);
                    });
            } catch (exception) { reject(exception); }
        });
    }
    

    注意:async/await 不是 promise 的替代品。如果你想创建一个 promise,你必须创建它。您可以使用 async/await 来避免 then 链,并且您的代码看起来像同步且易于理解。但它们仍然是异步的。

    但在您的情况下,您可以使用 Promise.resolve() 、 Promise.reject() 来避免使用新的 Promise()。见下文,

        async function GetData(testClient, project, testPlan, suiteId) {    
            console.debug("GetPoints");
            try {
              const data = await testClient.getData(project, testPlan, suiteId);                
              if (data.length === 0) return Promise.reject(data);
                return Promise.resolve(data);
    
            } catch (exception) { return Promise.reject(exception); }    
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-26
      • 1970-01-01
      • 2019-09-04
      相关资源
      最近更新 更多