"The keyword yield is reserved" eslint error

Brad picture Brad · Nov 26, 2016 · Viewed 8.5k times · Source

I'm attempting to use Webpack 1.13.12 and eslint 3.11.0 and eslint-plugin-promise 3.4.0. I'm trying to use the answer in this question to get Superagent to yield the result of a web service call.

import agent from 'superagent';
require('superagent-as-promised')(agent);
import Promise from 'promise';

const API_URL = 'http://localhost/services/merchant';

export function createVendorCall() {
    const responsePromise = yield Promise.resolve(agent.put(`${API_URL}/create`));

    let response = responsePromise.next();

    return response.body;
}

When I attempt to lint this, eslint complains that The keyword 'yield' is reserved. I've tried setting require-yield to 0 in my .eslintrc.json file, but it still won't lint. Using inline comments to disable eslint doesn't work either.

What should I do? Am I using Superagent the wrong way, or is there a rule I have to disable?

EDIT: This question was marked as a duplicate of this question. That question, however, was not using a linter and had a different error message. The problem here is that eslint is flagging what appears to be valid syntax as an error.

Answer

Eran Shabi picture Eran Shabi · Nov 26, 2016

Try adding a * to the function name so it will be a generator:

export function *createVendorCall() {
    const responsePromise = yield Promise.resolve(agent.put(`${API_URL}/create`));

    let response = responsePromise.next();

    return response.body;
}

yield should be used only in generators.