简体   繁体   English

如何在同步和异步代码的函数中拒绝承诺?

[英]How to reject a promise in a function of both sync and async code?

In my class I got a function that contains both sync and async code, something like:在我的课堂上,我得到了一个包含同步和异步代码的函数,例如:

export class SomeClass(){
    constructor(){
    }

    foo(a: SomeDataClass): Promise<void>{
        if(!a){
            throw new Error('a is undefined');
        }

        return doSomething(a.value).then(()=>{
            console.log('done');
        }).catch((error)=>{
            throw error;
        });
    }
}

export class SomeDataClass(){
    public value: number;
    public name: string;

    constructor(){
    }
}

Throwing the follow errors in the catch part would raise the catch for the user of foo function.catch部分抛出跟随错误将引发foo函数用户的catch but the first throw (in the if section) wont, in order to catch this i would have to surroun the use of foo within a try/catch.但是第一次抛出(在if部分)不会,为了捕捉到这个,我必须在 try/catch 中使用foo

How could i raise an error for the returned promise if i throw it not within a then section?如果我不在then部分中抛出它,我如何为返回的承诺引发错误? (Without using bluebird and rejecting a Resolver ) (不使用bluebird并拒绝Resolver

Please let me know if something is missing.如果有什么遗漏,请告诉我。

Thanks in advance for the help.在此先感谢您的帮助。

So I have found out there is a reject function on the Promise which actually does exactly what i was looking for, it raises an error for the function its used in as a Promise error and not like a regular throw to catch within a try/catch .所以我发现Promise上有一个reject函数,它实际上完全符合我的要求,它为用作Promise错误的函数引发错误,而不是像在try/catch的常规throw .

Returning this does the same a throwing an error in a Promise.then chains返回它与在Promise.then链中抛出错误相同

foo(a: SomeDataClass): Promise<void>{
    if(!a){
        return Promise.reject('a is undefined'); //<----- Promise.reject
    }

    return doSomething(a.value).then(()=>{
        console.log('done');
    }).catch((error)=>{
        throw error;
    });
}

暂无
暂无

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

相关问题 异步函数中的Promise reject()导致“未捕获(承诺)”警告 - Promise reject() in async function causes “Uncaught (in promise)” warning 如何在调用任何异步代码之前拒绝承诺(例如,在检测到无效参数时拒绝) - How do you reject a promise before calling any async code… (ex. reject upon detecting invalid arguments) 如何在不重新引发错误或不使用Promise.reject的情况下捕获来自异步函数的错误? - How to ensure an error from an async function is captured without rethrowing the error or using Promise.reject? 将带有解析和拒绝的承诺转换为异步函数的正确方法(puppeteer) - proper way to convert promise with resolve & reject to async function (puppeteer) 如何从内部拒绝 promise 然后 function - How to reject a promise from inside then function 如何仅使用本机Promise编写异步计数器,即带有同步接口的异步代码计数器? - How to write an async counter, i.e. a counter with sync interface for async code, using only the native Promise? 在Angular中,如何在使用异步/等待时处理承诺拒绝 - In Angular, how to handle promise reject when using async/await 如何在JavaScript中的异步Promise中使用同步? - How to use sync within an async Promise in JavaScript? 如何从express.router.post()中的功能代码返回承诺解决或拒绝? - How do I return a promise resolve or reject from a function code inside router.post() in express? 包装在promise与同步功能中的异步功能 - Async function wrapped in promise vs. sync function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM