1.0.3 • Published 8 years ago

fetch2 v1.0.3

Weekly downloads
1
License
ISC
Repository
github
Last release
8 years ago

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)
	}

	
1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago