Guest User

Untitled

a guest
Nov 23rd, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.20 KB | None | 0 0
  1. function foo() {
  2. var result;
  3.  
  4. $.ajax({
  5. url: '...',
  6. success: function(response) {
  7. result = response;
  8. // return response; // <- I tried that one as well
  9. }
  10. });
  11.  
  12. return result;
  13. }
  14.  
  15. var result = foo(); // It always ends up being `undefined`.
  16.  
  17. function findItem() {
  18. var item;
  19. while(item_not_found) {
  20. // search
  21. }
  22. return item;
  23. }
  24.  
  25. var item = findItem();
  26.  
  27. // Do something with item
  28. doSomethingElse();
  29.  
  30. findItem(function(item) {
  31. // Do something with item
  32. });
  33. doSomethingElse();
  34.  
  35. // Using 'superagent' which will return a promise.
  36. var superagent = require('superagent')
  37.  
  38. // This is isn't declared as `async` because it already returns a promise
  39. function delay() {
  40. // `delay` returns a promise
  41. return new Promise(function(resolve, reject) {
  42. // Only `delay` is able to resolve or reject the promise
  43. setTimeout(function() {
  44. resolve(42); // After 3 seconds, resolve the promise with value 42
  45. }, 3000);
  46. });
  47. }
  48.  
  49.  
  50. async function getAllBooks() {
  51. try {
  52. // GET a list of book IDs of the current user
  53. var bookIDs = await superagent.get('/user/books');
  54. // wait for 3 seconds (just for the sake of this example)
  55. await delay();
  56. // GET information about each book
  57. return await superagent.get('/books/ids='+JSON.stringify(bookIDs));
  58. } catch(error) {
  59. // If any of the awaited promises was rejected, this catch block
  60. // would catch the rejection reason
  61. return null;
  62. }
  63. }
  64.  
  65. // Async functions always return a promise
  66. getAllBooks()
  67. .then(function(books) {
  68. console.log(books);
  69. });
  70.  
  71. var result = foo();
  72. // Code that depends on 'result'
  73.  
  74. foo(function(result) {
  75. // Code that depends on 'result'
  76. });
  77.  
  78. function myCallback(result) {
  79. // Code that depends on 'result'
  80. }
  81.  
  82. foo(myCallback);
  83.  
  84. function foo(callback) {
  85. $.ajax({
  86. // ...
  87. success: callback
  88. });
  89. }
  90.  
  91. function foo(callback) {
  92. $.ajax({
  93. // ...
  94. success: function(response) {
  95. // For example, filter the response
  96. callback(filtered_response);
  97. }
  98. });
  99. }
  100.  
  101. function delay() {
  102. // `delay` returns a promise
  103. return new Promise(function(resolve, reject) {
  104. // Only `delay` is able to resolve or reject the promise
  105. setTimeout(function() {
  106. resolve(42); // After 3 seconds, resolve the promise with value 42
  107. }, 3000);
  108. });
  109. }
  110.  
  111. delay()
  112. .then(function(v) { // `delay` returns a promise
  113. console.log(v); // Log the value once it is resolved
  114. })
  115. .catch(function(v) {
  116. // Or do something else if it is rejected
  117. // (it would not happen in this example, since `reject` is not called).
  118. });
  119.  
  120. function ajax(url) {
  121. return new Promise(function(resolve, reject) {
  122. var xhr = new XMLHttpRequest();
  123. xhr.onload = function() {
  124. resolve(this.responseText);
  125. };
  126. xhr.onerror = reject;
  127. xhr.open('GET', url);
  128. xhr.send();
  129. });
  130. }
  131.  
  132. ajax("/echo/json")
  133. .then(function(result) {
  134. // Code depending on result
  135. })
  136. .catch(function() {
  137. // An error occurred
  138. });
  139.  
  140. function ajax() {
  141. return $.ajax(...);
  142. }
  143.  
  144. ajax().done(function(result) {
  145. // Code depending on result
  146. }).fail(function() {
  147. // An error occurred
  148. });
  149.  
  150. function checkPassword() {
  151. return $.ajax({
  152. url: '/password',
  153. data: {
  154. username: $('#username').val(),
  155. password: $('#password').val()
  156. },
  157. type: 'POST',
  158. dataType: 'json'
  159. });
  160. }
  161.  
  162. if (checkPassword()) {
  163. // Tell the user they're logged in
  164. }
  165.  
  166. checkPassword()
  167. .done(function(r) {
  168. if (r) {
  169. // Tell the user they're logged in
  170. } else {
  171. // Tell the user their password was bad
  172. }
  173. })
  174. .fail(function(x) {
  175. // Tell the user something bad happened
  176. });
  177.  
  178. function foo() {
  179. var jqXHR = $.ajax({
  180. //...
  181. async: false
  182. });
  183. return jqXHR.responseText;
  184. }
  185.  
  186. function foo() {
  187. var httpRequest = new XMLHttpRequest();
  188. httpRequest.open('GET', "/echo/json");
  189. httpRequest.send();
  190. return httpRequest.responseText;
  191. }
  192.  
  193. var result = foo(); // always ends up being 'undefined'
  194.  
  195. function getFive(){
  196. var a;
  197. setTimeout(function(){
  198. a=5;
  199. },10);
  200. return a;
  201. }
  202.  
  203. function onComplete(a){ // When the code completes, do this
  204. alert(a);
  205. }
  206.  
  207. function getFive(whenDone){
  208. var a;
  209. setTimeout(function(){
  210. a=5;
  211. whenDone(a);
  212. },10);
  213. }
  214.  
  215. getFive(onComplete);
  216.  
  217. var request = new XMLHttpRequest();
  218. request.open('GET', 'yourURL', false); // `false` makes the request synchronous
  219. request.send(null);
  220.  
  221. if (request.status === 200) {// That's HTTP for 'ok'
  222. console.log(request.responseText);
  223. }
  224.  
  225. var result = foo();
  226. // code that depends on `result` goes here
  227.  
  228. foo(function(result) {
  229. // code that depends on `result`
  230. });
  231.  
  232. function myHandler(result) {
  233. // code that depends on `result`
  234. }
  235. foo(myHandler);
  236.  
  237. function foo(callback) {
  238. var httpRequest = new XMLHttpRequest();
  239. httpRequest.onload = function(){ // when the request is loaded
  240. callback(httpRequest.responseText);// we're calling our method
  241. };
  242. httpRequest.open('GET', "/echo/json");
  243. httpRequest.send();
  244. }
  245.  
  246. function ajax(a, b, c){ // URL, callback, just a placeholder
  247. c = new XMLHttpRequest;
  248. c.open('GET', a);
  249. c.onload = b;
  250. c.send()
  251. }
  252.  
  253. this.response
  254.  
  255. e.target.response
  256.  
  257. function callback(e){
  258. console.log(this.response);
  259. }
  260. ajax('URL', callback);
  261.  
  262. ajax('URL', function(e){console.log(this.response)});
  263.  
  264. function x(a, b, e, d, c){ // URL, callback, method, formdata or {key:val},placeholder
  265. c = new XMLHttpRequest;
  266. c.open(e||'get', a);
  267. c.onload = b;
  268. c.send(d||null)
  269. }
  270.  
  271. x(url, callback); // By default it's get so no need to set
  272. x(url, callback, 'post', {'key': 'val'}); // No need to set post data
  273.  
  274. var fd = new FormData(form);
  275. x(url, callback, 'post', fd);
  276.  
  277. var fd = new FormData();
  278. fd.append('key', 'val')
  279. x(url, callback, 'post', fd);
  280.  
  281. function x(a, b, e, d, c){ // URL, callback, method, formdata or {key:val}, placeholder
  282. c = new XMLHttpRequest;
  283. c.open(e||'get', a);
  284. c.onload = b;
  285. c.onerror = error;
  286. c.send(d||null)
  287. }
  288.  
  289. function error(e){
  290. console.log('--Error--', this.type);
  291. console.log('this: ', this);
  292. console.log('Event: ', e)
  293. }
  294. function displayAjax(e){
  295. console.log(e, this);
  296. }
  297. x('WRONGURL', displayAjax);
  298.  
  299. function omg(a, c){ // URL
  300. c = new XMLHttpRequest;
  301. c.open('GET', a, true);
  302. c.send();
  303. return c; // Or c.response
  304. }
  305.  
  306. var res = omg('thisIsGonnaBlockThePage.txt');
  307.  
  308. function foo() {
  309. var data;
  310. // or $.get(...).then, or request(...).then, or query(...).then
  311. fetch("/echo/json").then(function(response){
  312. data = response.json();
  313. });
  314. return data;
  315. }
  316.  
  317. var result = foo(); // result is always undefined no matter what.
  318.  
  319. function delay(ms){ // takes amount of milliseconds
  320. // returns a new promise
  321. return new Promise(function(resolve, reject){
  322. setTimeout(function(){ // when the time is up
  323. resolve(); // change the promise to the fulfilled state
  324. }, ms);
  325. });
  326. }
  327.  
  328. function foo() {
  329. // RETURN the promise
  330. return fetch("/echo/json").then(function(response){
  331. return response.json(); // process it inside the `then`
  332. });
  333. }
  334.  
  335. foo().then(function(response){
  336. // access the value inside the `then`
  337. })
  338.  
  339. function* foo(){ // notice the star, this is ES6 so new browsers/node/io only
  340. yield 1;
  341. yield 2;
  342. while(true) yield 3;
  343. }
  344.  
  345. var foo = coroutine(function*(){
  346. var data = yield fetch("/echo/json"); // notice the yield
  347. // code here only executes _after_ the request is done
  348. return data.json(); // data is defined
  349. });
  350.  
  351. var main = coroutine(function*(){
  352. var bar = yield foo(); // wait our earlier coroutine, it returns a promise
  353. // server call done here, code below executes when done
  354. var baz = yield fetch("/api/users/"+bar.userid); // depends on foo's result
  355. console.log(baz); // runs after both requests done
  356. });
  357. main();
  358.  
  359. async function foo(){
  360. var data = await fetch("/echo/json"); // notice the await
  361. // code here only executes _after_ the request is done
  362. return data.json(); // data is defined
  363. }
  364.  
  365. function handleData( responseData ) {
  366.  
  367. // Do what you want with the data
  368. console.log(responseData);
  369. }
  370.  
  371. $.ajax({
  372. url: "hi.php",
  373. ...
  374. success: function ( data, status, XHR ) {
  375. handleData(data);
  376. }
  377. });
  378.  
  379. function callServerAsync(){
  380. $.ajax({
  381. url: '...',
  382. success: function(response) {
  383.  
  384. successCallback(response);
  385. }
  386. });
  387. }
  388.  
  389. function successCallback(responseObj){
  390. // Do something like read the response and show data
  391. alert(JSON.stringify(responseObj)); // Only applicable to JSON response
  392. }
  393.  
  394. function foo(callback) {
  395.  
  396. $.ajax({
  397. url: '...',
  398. success: function(response) {
  399. return callback(null, response);
  400. }
  401. });
  402. }
  403.  
  404. var result = foo(function(err, result){
  405. if (!err)
  406. console.log(result);
  407. });
  408.  
  409. promiseB = promiseA.then(
  410. function onSuccess(result) {
  411. return result + 1;
  412. }
  413. ,function onError(err) {
  414. //Handle error
  415. }
  416. );
  417.  
  418. // promiseB will be resolved immediately after promiseA is resolved
  419. // and its value will be the result of promiseA incremented by 1.
  420.  
  421. search(term: string) {
  422. return this.http
  423. .get(`https://api.spotify.com/v1/search?q=${term}&type=artist`)
  424. .map((response) => response.json())
  425. .toPromise();
  426.  
  427. search() {
  428. this.searchService.search(this.searchField.value)
  429. .then((result) => {
  430. this.result = result.artists.items;
  431. })
  432. .catch((error) => console.error(error));
  433. }
  434.  
  435. // WRONG
  436. var results = [];
  437. theArray.forEach(function(entry) {
  438. doSomethingAsync(entry, function(result) {
  439. results.push(result);
  440. });
  441. });
  442. console.log(results); // E.g., using them, returning them, etc.
  443.  
  444. var results = [];
  445. var expecting = theArray.length;
  446. theArray.forEach(function(entry, index) {
  447. doSomethingAsync(entry, function(result) {
  448. results[index] = result;
  449. if (--expecting === 0) {
  450. // Done!
  451. console.log("Results:", results); // E.g., using the results
  452. }
  453. });
  454. });
  455.  
  456. function doSomethingWith(theArray, callback) {
  457. var results = [];
  458. var expecting = theArray.length;
  459. theArray.forEach(function(entry, index) {
  460. doSomethingAsync(entry, function(result) {
  461. results[index] = result;
  462. if (--expecting === 0) {
  463. // Done!
  464. callback(results);
  465. }
  466. });
  467. });
  468. }
  469. doSomethingWith(theArray, function(results) {
  470. console.log("Results:", results);
  471. });
  472.  
  473. function doSomethingWith(theArray) {
  474. return new Promise(function(resolve) {
  475. var results = [];
  476. var expecting = theArray.length;
  477. theArray.forEach(function(entry, index) {
  478. doSomethingAsync(entry, function(result) {
  479. results[index] = result;
  480. if (--expecting === 0) {
  481. // Done!
  482. resolve(results);
  483. }
  484. });
  485. });
  486. });
  487. }
  488. doSomethingWith(theArray).then(function(results) {
  489. console.log("Results:", results);
  490. });
  491.  
  492. function doSomethingWith(theArray) {
  493. return Promise.all(theArray.map(function(entry) {
  494. return doSomethingAsync(entry, function(result) {
  495. results.push(result);
  496. });
  497. }));
  498. }
  499. doSomethingWith(theArray).then(function(results) {
  500. console.log("Results:", results);
  501. });
  502.  
  503. function doSomethingWith(theArray, callback) {
  504. var results = [];
  505. doOne(0);
  506. function doOne(index) {
  507. if (index < theArray.length) {
  508. doSomethingAsync(theArray[index], function(result) {
  509. results.push(result);
  510. doOne(index + 1);
  511. });
  512. } else {
  513. // Done!
  514. callback(results);
  515. }
  516. }
  517. }
  518. doSomethingWith(theArray, function(results) {
  519. console.log("Results:", results);
  520. });
  521.  
  522. async function doSomethingWith(theArray) {
  523. const results = [];
  524. for (const entry of theArray) {
  525. results.push(await doSomethingAsync(entry));
  526. }
  527. return results;
  528. }
  529. doSomethingWith(theArray).then(results => {
  530. console.log("Results:", results);
  531. });
  532.  
  533. function doSomethingWith(theArray) {
  534. return theArray.reduce(function(p, entry) {
  535. return p.then(function(results) {
  536. return doSomethingAsync(entry).then(function(result) {
  537. results.push(result);
  538. return results;
  539. });
  540. });
  541. }, Promise.resolve([]));
  542. }
  543. doSomethingWith(theArray).then(function(results) {
  544. console.log("Results:", results);
  545. });
  546.  
  547. function doSomethingWith(theArray) {
  548. return theArray.reduce((p, entry) => p.then(results => doSomethingAsync(entry).then(result => {
  549. results.push(result);
  550. return results;
  551. })), Promise.resolve([]));
  552. }
  553. doSomethingWith(theArray).then(results => {
  554. console.log("Results:", results);
  555. });
  556.  
  557. var app = angular.module('plunker', []);
  558.  
  559. app.controller('MainCtrl', function($scope,$http) {
  560.  
  561. var getJoke = function(){
  562. return $http.get('http://api.icndb.com/jokes/random').then(function(res){
  563. return res.data.value;
  564. });
  565. }
  566.  
  567. getJoke().then(function(res) {
  568. console.log(res.joke);
  569. });
  570. });
  571.  
  572. (function(){
  573. async function getJoke(){
  574. let response = await fetch('http://api.icndb.com/jokes/random');
  575. let data = await response.json();
  576. return data.value;
  577. }
  578.  
  579. getJoke().then((joke) => {
  580. console.log(joke);
  581. });
  582. })();
  583.  
  584. var async = require("async");
  585.  
  586. // This wires up result back to the caller
  587. var result = {};
  588. var asyncTasks = [];
  589. asyncTasks.push(function(_callback){
  590. // some asynchronous operation
  591. $.ajax({
  592. url: '...',
  593. success: function(response) {
  594. result.response = response;
  595. _callback();
  596. }
  597. });
  598. });
  599.  
  600. async.parallel(asyncTasks, function(){
  601. // result is available after performing asynchronous operation
  602. console.log(result)
  603. console.log('Done');
  604. });
  605.  
  606. $(document).ready(function(){
  607. function foo() {
  608. $.ajax({url: "api/data", success: function(data){
  609. fooDone(data); //after we have data, we pass it to fooDone
  610. }});
  611. };
  612.  
  613. function fooDone(data) {
  614. console.log(data); //fooDone has the data and console.log it
  615. };
  616.  
  617. foo(); //call happens here
  618. });
  619.  
  620. if (!name) {
  621. name = async1();
  622. }
  623. async2(name);
  624.  
  625. async1(name, callback) {
  626. if (name)
  627. callback(name)
  628. else {
  629. doSomething(callback)
  630. }
  631. }
  632.  
  633. async1(name, async2)
  634.  
  635. var Fiber = require('fibers')
  636.  
  637. function async1(container) {
  638. var current = Fiber.current
  639. var result
  640. doSomething(function(name) {
  641. result = name
  642. fiber.run()
  643. })
  644. Fiber.yield()
  645. return result
  646. }
  647.  
  648. Fiber(function() {
  649. var name
  650. if (!name) {
  651. name = async1()
  652. }
  653. async2(name)
  654. // Make any number of async calls from here
  655. }
  656.  
  657. function callback(response) {
  658. // Here you can do what ever you want with the response object.
  659. console.log(response);
  660. }
  661.  
  662. $.ajax({
  663. url: "...",
  664. success: callback
  665. });
  666.  
  667. [
  668. "search?type=playlist&q=%22doom%20metal%22",
  669. "search?type=playlist&q=Adele"
  670. ]
  671.  
  672. -H "Authorization: Bearer {your access token}"
  673.  
  674. (async function(){
  675.  
  676. var response = await superagent.get('...')
  677. console.log(response)
  678.  
  679. })()
  680.  
  681. function $http(apiConfig) {
  682. return new Promise(function (resolve, reject) {
  683. var client = new XMLHttpRequest();
  684. client.open(apiConfig.method, apiConfig.url);
  685. client.send();
  686. client.onload = function () {
  687. if (this.status >= 200 && this.status < 300) {
  688. // Performs the function "resolve" when this.status is equal to 2xx.
  689. // Your logic here.
  690. resolve(this.response);
  691. }
  692. else {
  693. // Performs the function "reject" when this.status is different than 2xx.
  694. reject(this.statusText);
  695. }
  696. };
  697. client.onerror = function () {
  698. reject(this.statusText);
  699. };
  700. });
  701. }
  702.  
  703. $http({
  704. method: 'get',
  705. url: 'google.com'
  706. }).then(function(response) {
  707. console.log(response);
  708. }, function(error) {
  709. console.log(error)
  710. });
  711.  
  712. var ajaxGet = function (ctx,url) {
  713. var res = {};
  714. var ex;
  715. $.ajax(url)
  716. .done(function (data) {
  717. res.data = data;
  718. })
  719. .fail(function(e) {
  720. ex = e;
  721. })
  722. .always(function() {
  723. ctx.resume(ex);
  724. });
  725. return res;
  726. };
  727. ajaxGet.nsynjsHasCallback = true;
  728.  
  729. function process() {
  730. console.log('got data:', ajaxGet(nsynjsCtx, "data/file1.json").data);
  731. }
  732.  
  733. nsynjs.run(process,this,function () {
  734. console.log("synchronous function finished");
  735. });
  736.  
  737. function doAjax(callbackFunc, method, url) {
  738. var xmlHttpReq = new XMLHttpRequest();
  739. xmlHttpReq.open(method, url);
  740. xmlHttpReq.onreadystatechange = function() {
  741.  
  742. if (xmlHttpReq.readyState == 4 && xmlHttpReq.status == 200) {
  743. callbackFunc(xmlHttpReq.responseText);
  744. }
  745.  
  746.  
  747. }
  748. xmlHttpReq.send(null);
  749.  
  750. }
  751.  
  752. function loadMyJson(categoryValue){
  753. if(categoryValue==="veg")
  754. doAjax(print,"GET","http://localhost:3004/vegetables");
  755. else if(categoryValue==="fruits")
  756. doAjax(print,"GET","http://localhost:3004/fruits");
  757. else
  758. console.log("Data not found");
  759. }
  760.  
  761. function foo(){
  762. // do something
  763. return 'wohoo';
  764. }
  765.  
  766. let bar = foo(); // bar is 'wohoo' here
  767.  
  768. function foo(){
  769. setTimeout( ()=>{
  770. return 'wohoo';
  771. }, 1000 )
  772. }
  773.  
  774. let bar = foo() // bar is undefined here
  775.  
  776. function foo(){
  777. return new Promise( (resolve, reject) => { // I want foo() to PROMISE me something
  778. setTimeout ( function(){
  779. // promise is RESOLVED , when execution reaches this line of code
  780. resolve('wohoo')// After 1 second, RESOLVE the promise with value 'wohoo'
  781. }, 1000 )
  782. })
  783. }
  784.  
  785. let bar ;
  786. foo().then( res => {
  787. bar = res;
  788. console.log(bar) // will print 'wohoo'
  789. });
  790.  
  791. function fetchUsers(){
  792. let users = [];
  793. getUsers()
  794. .then(_users => users = _users)
  795. .catch(err =>{
  796. throw err
  797. })
  798. return users;
  799. }
  800.  
  801. async function fetchUsers(){
  802. try{
  803. let users = await getUsers()
  804. return users;
  805. }
  806. catch(err){
  807. throw err;
  808. }
  809. }
  810.  
  811. function* myGenerator() {
  812. const callback = yield;
  813. let [response] = yield $.ajax("https://stackoverflow.com", {complete: callback});
  814. console.log("response is:", response);
  815.  
  816. // examples of other things you can do
  817. yield setTimeout(callback, 1000);
  818. console.log("it delayed for 1000ms");
  819. while (response.statusText === "error") {
  820. [response] = yield* anotherGenerator();
  821. }
  822. }
  823.  
  824. const gen = myGenerator(); // Create generator
  825. gen.next(); // Start it
  826. gen.next((...args) => gen.next([...args])); // Set its callback function
  827.  
  828. const [err, data] = yield fs.readFile(filePath, "utf-8", callback);
  829.  
  830. var lat = "";
  831. var lon = "";
  832. function callback(data) {
  833. lat = data.lat;
  834. lon = data.lon;
  835. }
  836. function getLoc() {
  837. var url = "http://ip-api.com/json"
  838. $.getJSON(url, function(data) {
  839. callback(data);
  840. });
  841. }
  842.  
  843. getLoc();
  844.  
  845. function foo(result) {
  846. $.ajax({
  847. url: '...',
  848. success: function(response) {
  849. result.response = response; // Store the async result
  850. }
  851. });
  852. }
  853.  
  854. var result = { response: null }; // Object to hold the async result
  855. foo(result); // Returns before the async completes
  856.  
  857. function foo() {
  858. var result;
  859.  
  860. $.ajax({
  861. url: '...',
  862. success: function(response) {
  863. myCallback(response);
  864. }
  865. });
  866.  
  867. return result;
  868. }
  869.  
  870. function myCallback(response) {
  871. // Does something.
  872. }
  873.  
  874. __pragma__ ('alias', 'S', '$')
  875.  
  876. def read(url: str):
  877. deferred = S.Deferred()
  878. S.ajax({'type': "POST", 'url': url, 'data': { },
  879. 'success': lambda d: deferred.resolve(d),
  880. 'error': lambda e: deferred.reject(e)
  881. })
  882. return deferred.promise()
  883.  
  884. async def readALot():
  885. try:
  886. result1 = await read("url_1")
  887. result2 = await read("url_2")
  888. except Exception:
  889. console.warn("Reading a lot failed")
  890.  
  891. var milk = order_milk();
  892. put_in_coffee(milk);
  893.  
  894. order_milk(put_in_coffee);
  895.  
  896. order_milk(function(milk) { put_in_coffee(milk, drink_coffee); }
  897.  
  898. var answer;
  899. $.ajax('/foo.json') . done(function(response) {
  900. callback(response.data);
  901. });
  902.  
  903. function callback(data) {
  904. console.log(data);
  905. }
  906.  
  907. order_milk() . then(put_in_coffee)
  908.  
  909. order_milk() . then(put_in_coffee) . then(drink_coffee)
  910.  
  911. function get_data() {
  912. return $.ajax('/foo.json');
  913. }
  914.  
  915. get_data() . then(do_something)
  916.  
  917. get_data() .
  918. then(function(data) { console.log(data); });
  919.  
  920. get_data() .
  921. then(data => console.log(data));
  922.  
  923. a();
  924. b();
  925.  
  926. a() . then(b);
  927.  
  928. async function morning_routine() {
  929. var milk = await order_milk();
  930. var coffee = await put_in_coffee(milk);
  931. await drink(coffee);
  932. }
  933.  
  934. async function foo() {
  935. data = await get_data();
  936. console.log(data);
  937. }
  938.  
  939. async function foo() {
  940. var response = await $.ajax({url: '...'})
  941. return response;
  942. }
  943.  
  944. (async function() {
  945. try {
  946. var result = await foo()
  947. console.log(result)
  948. } catch (e) {}
  949. })()
  950.  
  951. foo().then(response => {
  952. console.log(response)
  953.  
  954. }).catch(error => {
  955. console.log(error)
  956.  
  957. })
  958.  
  959. while (queue.waitForMessage()) {
  960. queue.processNextMessage();
  961. }
  962.  
  963. 1. call foo.com/api/bar using foobarFunc
  964. 2. Go perform an infinite loop
  965. ... and so on
  966.  
  967. function foobarFunc (var) {
  968. console.log(anotherFunction(var));
  969. }
  970.  
  971. function foo(bla) {
  972. console.log(bla)
  973. }
Add Comment
Please, Sign In to add comment