Code Monkey home page Code Monkey logo

Comments (5)

analog-nico avatar analog-nico commented on July 28, 2024

Hi @Vietworm , your code looks good already. I would further improve the way you use the promises:

rp(opt)
    .then(function (result) {
        let opt2 = { /* with data from result */ };
        return rp(opt2); // The `return` inserts the second request into the promise chain.
    })
    .then(function (resultOfSecondRequest) {
        // Process resultOfSecondRequest here...
    })
    .catch(function (err) {
        // Handle any error from the first, the second request, or the final processing here...
    });

Does this help?

from request-promise.

lephuhai avatar lephuhai commented on July 28, 2024

@analog-nico thanks for answer :)

I've been tried it but throw error

Unhandled rejection RequestError: Error: Argument error, options.body.

and more, after ended last request. I need results of all request before.

from request-promise.

analog-nico avatar analog-nico commented on July 28, 2024

Since it says Argument error, options.body you got something wrong with the body option. You find some details about this option here.

Since it is also saying Unhandled rejection means that you did not properly add a .catch(...) handler. My code above does this properly.

If you need both results then you may use:

rp(opt)
    .then(function (result) {
        let opt2 = { /* with data from result */ };
        return [result, rp(opt2)];
    })
    .spread(function (resultOfFirstRequest, resultOfSecondRequest) {
        // Process both results here...
    })
    .catch(function (err) {
        // Handle any error from the first, the second request, or the final processing here...
    });

from request-promise.

lephuhai avatar lephuhai commented on July 28, 2024

Thanks so much @analog-nico.
My fault, i defined options not enough. As the forgotten json: true option.

from request-promise.

analog-nico avatar analog-nico commented on July 28, 2024

You are welcome.

from request-promise.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.