Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SwiftのAsync / Awaitについて

SwiftのAsync / Awaitについて

第1回 HAKATA.swift ~福岡でSwiftの勉強会~

Takaaki Tanaka

April 14, 2018
Tweet

More Decks by Takaaki Tanaka

Other Decks in Technology

Transcript

  1. 1SPNJTF doSomething().then(function(result) { return doSomethingElse(result); }) .then(function(newResult) { return doThirdThing(newResult);

    }) .then(function(finalResult) { console.log('Got the final result: ' + finalResult); }) .catch(failureCallback);
  2. 'VUVSF val f: Future[List[String]] = Future { session.getRecentPosts } f

    onFailure { case t => println("Τϥʔ͕ൃੜͨ͠: " + t.getMessage) } f onSuccess { case posts => for (post <- posts) println(post) }
  3. wඇಉظͰฦ͞ΕΔ஋Λड͚ΔͨΊͷߏจ "XBJU function resolveAfter2Seconds(x) { return new Promise(resolve => {

    setTimeout(() => { resolve(x); }, 2000); }); } async function f1() { var x = await resolveAfter2Seconds(10); console.log(x); // 10 } f1();
  4. wඇಉظͰ஋Λฦ͢͜ͱΛ໌ࣔ͢Δߏจ "TZOD GVODUJPOSFTPMWF"GUFS4FDPOET \ SFUVSOOFX1SPNJTF SFTPMWF\ TFU5JNFPVU \ SFTPMWF SFTPMWFE

     ^   ^  ^ BTZODGVODUJPOBTZOD$BMM \ DPOTPMFMPH DBMMJOH  WBSSFTVMUBXBJUSFTPMWF"GUFS4FDPOET  DPOTPMFMPH SFTVMU  FYQFDUFEPVUQVUSFTPMWFE ^ BTZOD$BMM 
  5. "TZOD function resolveAfter2Seconds(x) { return new Promise(resolve => { setTimeout(()

    => { resolve(x); }, 2000); }); } async function add1(x) { const a = await resolveAfter2Seconds(20); const b = await resolveAfter2Seconds(30); return x + a + b; } add1(10).then(v => { console.log(v); // 4ඵޙʹ60Λදࣔ͠·͢ɻ });