简体   繁体   English

ReactJS 中的异步/等待启动

[英]Async/Await inception in ReactJS

I'm having a problem with using async/await promises and then using those values later on.我在使用 async/await 承诺然后稍后使用这些值时遇到问题。

The main example that I've seen is:我见过的主要例子是:

const myAsyncFunction = async () => {
    const resultA = await promiseA();
    const resultB = await promiseB();

    return functionA() // something that uses both resultA and resultB
}

This was quite helpful to point me in the right direction, but I still cannot get my app to work how I want it.这对指出我正确的方向很有帮助,但我仍然无法让我的应用程序按我想要的方式工作。 The thing is that I need resultA in promiseB .问题是我需要resultApromiseB

I tried to accomplish this by doing some async function inception, like so:我试图通过做一些异步 function 开始来实现这一点,如下所示:

const myAsyncFunction = async () => {
    const resultA = await promiseA();

    return async () => {
        const resultB = await promiseB();

        return functionA() // something that uses both resultA and resultB
    }
}

Alas, this does not seem to work either.唉,这似乎也不起作用。 functionA is also an async function, but I do not need the value that it returns. functionA也是一个异步 function,但我不需要它返回的值。 When I run this code, it appears that functionA does not run at all.当我运行此代码时,似乎functionA根本没有运行。

Hope somebody can help me.希望有人可以帮助我。 Thanks in advance.提前致谢。

doRegisterWithEmailPasswordAndPhoto = async (
        email: string,
        password: string,
        firstname: string,
        lastname: string,
        username: string,
        photo: File | null
    ) => {
        const photoUploadTask: firebase.storage.UploadTask = await this.storage
            .ref()
            .child('profile-pictures/' + username)
            .put(photo ? photo : exampleProfilePic);

        const userCredential = await this.auth.createUserWithEmailAndPassword(
            email,
            password
        );

        const finalDataPush = async (photoUploadTask: Promise<firebase.storage.UploadTask>) => {

            await userCredential.user?.updateProfile({
                displayName: firstname + ' ' + lastname,
                photoURL: (await photoUploadTask).snapshot,
            });

            return this.db.collection('users').doc(userCredential.user.uid).set({
                firstname: firstname,
                lastname: lastname,
                username: username,
                email: email,
                admin: false,
                forumPosts: 0,
                created: this.dbFunc.FieldValue.serverTimestamp(),
                photoURL: photoUploadTask.snapshot.downloadURL,
            });
        };

        return finalDataPush(photoUploadTask);
    };

Promises are a red herring here.承诺在这里是一个红鲱鱼。 If you need a value in a function, then pass it as an argument.如果您需要 function 中的值,则将其作为参数传递。

const myAsyncFunction = async () => {
    const resultA = await promiseA();
    const resultB = await promiseB(resultA);

    return functionA(resultA, resultB) // something that uses both resultA and resultB
}

Obviously you need to write your promise-generating functions to expect arguments.显然,您需要编写您的承诺生成函数来期待 arguments。

You don't really need that finalDataPush helper function.你真的不需要finalDataPush助手 function。 The following should just work:以下应该只是工作:

async function doRegisterWithEmailPasswordAndPhoto(
    email: string,
    password: string,
    firstname: string,
    lastname: string,
    username: string,
    photo: File | null
) {
    const photoUploadTask: firebase.storage.UploadTask = await this.storage
        .ref()
        .child('profile-pictures/' + username)
        .put(photo ? photo : exampleProfilePic);

    const userCredential = await this.auth.createUserWithEmailAndPassword(
        email,
        password
    );

    await userCredential.user?.updateProfile({
        displayName: firstname + ' ' + lastname,
        photoURL: photoUploadTask.snapshot,
    });

    return this.db.collection('users').doc(userCredential.user.uid).set({
        firstname: firstname,
        lastname: lastname,
        username: username,
        email: email,
        admin: false,
        forumPosts: 0,
        created: this.dbFunc.FieldValue.serverTimestamp(),
        photoURL: photoUploadTask.snapshot.downloadURL,
    });
}

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

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