This file contains hidden or 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
async function loadData() { | |
try { | |
var data = JSON.parse(await getJSON()); | |
console.log(data); | |
} catch(e) { | |
console.log(e); | |
} | |
} |
This file contains hidden or 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
function loadData() { | |
return getJSON() | |
.then(function(response) { | |
if (response.needsAnotherRequest) { | |
return makeAnotherRequest(response) | |
.then(function(anotherResponse) { | |
console.log(anotherResponse) | |
return anotherResponse | |
}) | |
} else { |
This file contains hidden or 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
async function loadData() { | |
var response = await getJSON(); | |
if (response.needsAnotherRequest) { | |
var anotherResponse = await makeAnotherRequest(response); | |
console.log(anotherResponse) | |
return anotherResponse | |
} else { | |
console.log(response); | |
return response; | |
} |
This file contains hidden or 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
function loadData() { | |
return callAPromise() | |
.then(callback1) | |
.then(callback2) | |
.then(callback3) | |
.then(() => { | |
throw new Error("boom"); | |
}) | |
} | |
loadData() |
This file contains hidden or 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
async function loadData() { | |
await callAPromise1() | |
await callAPromise2() | |
await callAPromise3() | |
await callAPromise4() | |
await callAPromise5() | |
throw new Error("boom"); | |
} | |
loadData() | |
.catch(function(e) { |
This file contains hidden or 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
(function poll(){ | |
setTimeout(function(){ | |
$.ajax({ | |
url: 'https://api.example.com/endpoint', | |
success: function(data) { | |
// Do something with `data` | |
// ... | |
//Setup the next poll recursively | |
poll(); |
This file contains hidden or 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
// Create a new WebSocket with an encrypted connection. | |
var socket = new WebSocket('ws://websocket.example.com'); |
This file contains hidden or 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
var socket = new WebSocket('ws://websocket.example.com'); | |
// Show a connected message when the WebSocket is opened. | |
socket.onopen = function(event) { | |
console.log('WebSocket is connected.'); | |
}; |
This file contains hidden or 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
var socket = new WebSocket('ws://websocket.example.com'); | |
socket.onopen = function(event) { | |
socket.send('Some message'); // Sends data to server. | |
}; |
This file contains hidden or 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
// We'll be using the https://github.com/theturtle32/WebSocket-Node | |
// WebSocket implementation | |
var WebSocketServer = require('websocket').server; | |
var http = require('http'); | |
var server = http.createServer(function(request, response) { | |
// process HTTP request. | |
}); | |
server.listen(1337, function() { }); |