fetch2

1.0.3 • Public • Published

fetch2

Promise package using fetch support cascade operation

use

    npm install fetch2
    let fetch2 = require("fetch2")
    var result = yield fetch2(url).json() //use * yield

use fetch2


fetch2("http://www.baidu.com").text().then((body)=>{
    console.log(body)
})

  • use co

co(function *(){

    var body = yield fetch2("http://www.baidu.com").text();
    console.log(body)

})

  • use * function

function *abc(){

    var body = yield fetch2("http://www.baidu.com").text();
    console.log(body)

}

  • use async await

async function abc(){

    var body = await fetch2("http://www.baidu.com").text();
    console.log(body)

}

abc()

更多API

    
    fetch2(url).text() // return ==> response.text()  body.toString()
    fetch2(url).json() // return json ==> response.json() JSON.parse(body)
    fetch2(url).heade(name) // return get ==> response.headers[name]
    fetch2(url).headers() // return all ==> response.headers

example use async await or co or *


    api  /getUser
    return {
        firstName:"lu",
        lastName:"jun"
    }

    async function renderUser(){

        var user = await fetch2(`${domain}/getUser`).json()

        console.log(user.firstName) // "lu"
    }

    renderUser()

  • base

    api  /getUser
    return {
        firstName:"lu",
        lastName:"jun"
    }

    
    fetch2(`${domain}/getUser`).json().then((user)=>{
        console.log(user.firstName) // "lu"
    }).catch(e){
        console.log(e)
    }

    

Readme

Keywords

none

Package Sidebar

Install

npm i fetch2

Weekly Downloads

4

Version

1.0.3

License

ISC

Last publish

Collaborators

  • jun.lu