Created
February 26, 2017 00:45
-
-
Save kenmori/85495c4e0577412ab030888ffa39e2ef to your computer and use it in GitHub Desktop.
async_await2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react' | |
import ReactDOM from 'react-dom' | |
import 'babel-polyfill'; | |
class Greet extends Component { | |
render() { | |
function hidouki(url, num){ | |
return new Promise(function(resolve, reject){ | |
var req = new XMLHttpRequest(); | |
req.open('GET', url); | |
req.onload = function () { | |
console.log(num) | |
if(req.status == 200){ | |
resolve(req.response); | |
} else { | |
reject(Error(req.statusText)); | |
} | |
} | |
req.onerror = function(){ | |
reject(Error("Network Error")) | |
} | |
req.send(); | |
}) | |
} | |
async function asyncFunction (){ | |
const result = await hidouki('http://localhost:3000/posts', 1); | |
console.log(result); | |
const result2 = await hidouki('http://localhost:3000/comments', 2); | |
console.log(result2); | |
const result3 = await hidouki('http://localhost:3000/profile', 3); | |
console.log(result3); | |
const result4 = await hidouki('http://localhost:3000/state', 4); | |
console.log(result4); | |
console.log('done!'); | |
} | |
asyncFunction(); | |
return ( | |
<ul id='result'></ul> | |
) | |
} | |
} | |
window.addEventListener('DOMContentLoaded', ()=>{ | |
ReactDOM.render(<Greet />, document.querySelector('main')); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment