简体   繁体   English

yield是异步函数内部的保留关键字错误

[英]yield is a reserved keyword error inside async function

I need to use window.chrome.storage to get the user email from the chrome storage and check if that email matches with the user logged in email from the response.data.email. 我需要使用window.chrome.storage从chrome存储中获取用户电子邮件,并检查该电子邮件是否与从response.data.email登录的电子邮件匹配。 if it matches, then dispatch success function else error. 如果匹配,则分派成功函数,否则返回错误。 However, I am getting an error of 'yield is a reserved keyword'. 但是,我收到一个错误消息,“ yield是保留关键字”。 How do i then make it work? 我该如何运作?

here is what i have done 这是我所做的

function* setSessionAndLogin(response, headers, successCB, failureCB) {
    if (response.data) {
        const sessionValue = Array.from(headers.entries()).reduce(
            (val, entry) => ({ ...val, [entry[0]]: entry[1] }),
            {}
        );

    // const results = yield call(() => {})
    window.chrome.storage.sync.get(['user_email'], result => {
      if (response.data.email === result.user_email) {
        yield put(successCB(response.data));
        window.chrome.storage.sync.set(
                { user_token: btoa(JSON.stringify(sessionValue)) },
                function() {}
            );
      } else {
            yield put(failureCB('Email does not match'));
        }
    });

    } else {
        yield put(failureCB(response.errors[0]));
    }
}

UPDATE 更新

Solutions i tried 我尝试过的解决方案

solution 1. 解决方案1。

Here nothing happens. 这里什么也没有发生。 I don't get error but also console.log('result', result, result.user_email); 我没有错误,但是也没有console.log('result',result,result.user_email); does not prints anything in the console. 在控制台中不打印任何内容。

function* setSessionAndLogin(response, headers, successCB, failureCB) {
    if (response.data) {
        const sessionValue = Array.from(headers.entries()).reduce(
            (val, entry) => ({ ...val, [entry[0]]: entry[1] }),
            {}
        );
        window.chrome.storage.sync.get(['user_email'], function*(result) {
            console.log('result', result, result.user_email);
            if (result.user_email && response.data.email === result.user_email) {
                yield put(successCB(response.data));
                window.chrome.storage.sync.set(
                    { user_token: btoa(JSON.stringify(sessionValue)) },
                    function() {}
                );
            } else {
                yield put(failureCB('Email does not match'));
            }
        });
    } else {
        console.log('error');
        yield put(failureCB(response.errors[0]));
    }
}

solution 2 解决方案2

const results = yield call(fetchUserEmail)
async function fetchUserEmail() {
    let userEmail = [];
    const email = await window.chrome.storage.sync.get(['user_email'], result => {
        userEmail.push(result.user_email);
        console.log('userEmail', userEmail);
    });

    return userEmail;
}

here i get empty array. 在这里,我得到空数组。

I solved this using promise. 我用诺言解决了这个问题。 If async operation has to be carried inside generator, one option you can do is the following way. 如果必须在生成器内部进行异步操作,则可以采用以下方法之一。

call the function using yield call(fetchUserEmail) . 使用yield call(fetchUserEmail)调用函数。 This is a bit readable and organized way to do in some extent. 在某种程度上,这是一种易读且有组织的方法。 I do not know about async/await so the answer in async/await is highly appreciated. 我不了解异步/等待,所以高度赞赏异步/等待中的答案。 Solution in different ways gives different idea to solve the problem and thus increases the knowledge on various domain. 以不同的方式解决问题会给出解决问题的不同思路,从而增加了各个领域的知识。

function fetchUserEmail() {
    return new Promise((resolve, reject) => {
        let userEmail = [];
        window.chrome.storage.sync.get(['user_email'], result => {
            userEmail.push(result.user_email);
            resolve(userEmail);
        });
    });
}

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

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