Vietnam

    Nodejs.vn

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Tags
    • Groups
    • Search

    Hỏi về promise

    Hỏi Đáp
    0
    3
    371
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • T
      Tran Vj last edited by

      có cách nào lôi kết quả ra khỏi ngoài then trong defer không mọi người http://prntscr.com/jr2llv, e có hỏi ngu thì bỏ qua giúp e

      Vũ 1 Reply Last reply Reply Quote 0
      • Vũ
        Vũ Global Moderator @Tran Vj last edited by

        @Tran-Vj Bạn sử dụng Promise kết hợp cú pháp ES7: await là được.

        Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await
        Ví dụ:

        function resolveAfter2Seconds(x) { 
          return new Promise(resolve => {
            setTimeout(() => {
              resolve(x);
            }, 2000);
          });
        }
        
        async function f1() {
          var x = await resolveAfter2Seconds(10);
          console.log(x); // 10
        }
        f1();
        ``

        Tech-nông
        Email: [email protected]
        Profile: about.me/vunb
        Github: github.com/vunb
        Twitter: twitter.com/nhubaovu

        1 Reply Last reply Reply Quote 1
        • T
          Tran Vj last edited by

          dạ , em làm được rồi, tks a

          1 Reply Last reply Reply Quote 0
          • First post
            Last post