Vietnam

    Nodejs.vn

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

    Code chạy tuần tự các công việc trong mảng

    Hỏi Đáp
    0
    9
    963
    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.
    • N
      Nguyen Quan last edited by Nguyen Quan

      const arrMovie = ['movie1.mp4', 'movie2.mp4', 'movie3.mp4'];
      arrMovie.map( async (movie) => {
      await cutVideo(movie);
      });
      nếu mình code như vầy thì nó cắt cả 3 video đồng thời nên mình muốn hỏi các bác có cách nào cắt tuần tự hết cái này xong mới đến cái kia ko?

      Vũ 1 Reply Last reply Reply Quote 0
      • Kha Pham
        Kha Pham last edited by

        Bạn thử tạo hạm cutVideo thành promise. Mình củng từng bị như thế. khi chuyển thành promise thì async await mới hoạt động
        0_1527998143202_image.png

        Đam mê công nghệ
        Không biết nhiều nhưng cũng muốn đóng góp

        1 Reply Last reply Reply Quote 0
        • Vũ
          Vũ Global Moderator @Nguyen Quan last edited by

          @Nguyen-Quan Bạn dùng hàm each trong thư viện async mình nghĩ ok.

          Ví dụ trường hợp của bạn có thể áp dụng thế này:

          const arrMovie = ['movie1.mp4', 'movie2.mp4', 'movie3.mp4'];
          
          async.each(arrMovie , async (fileVideo, callback) => {
          
              // Perform operation on file here.
              console.log('Processing file ' + fileVideo);
              var result = await cutVideo(movie);
              console.log('Processing done.')
              callback();
          }, function(err) {
              // if any of the file processing produced an error, err would equal that error
              if( err ) {
                // One of the iterations produced an error.
                // All processing will now stop.
                console.log('A file failed to process');
              } else {
                console.log('All files have been processed successfully');
              }
          });
          

          @Kha-Pham Trường hợp của bạn là xử lý 1 việc dạng bất đồng bộ. Nhưng tình huống có nhiều task thì đơn thuần Promise ko xử lý được.

          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
          • Duc Hoang
            Duc Hoang last edited by Duc Hoang

            Bạn nên dùng hàm reduce để chain (nối) các promise lại. Code rất đơn giản như sau:

            arrMovie.reduce((currentPromise, currentMovie) => currentPromise.then(()=>return cutVideo(currentMovie)), Promise.resolve())

            Đoạn code trên nó sẽ tương ứng như sau:

            Promise.resolve()
            .then(()=>cutVideo('movie1.mp4'))
            .then(()=>cutVideo('movie2.mp4'))
            .then(()=>cutVideo('movie3.mp4))
            .then ...

            Vũ 1 Reply Last reply Reply Quote 2
            • Vũ
              Vũ Global Moderator @Duc Hoang last edited by

              @Duc-Hoang Cách của bạn hay quá!

              Nhưng hình như thừa lệnh return trong ví dụ mẫu.

              // sử dụng reduce để tạo promise chain.
              var tasks = arrMovie.reduce((currentPromise, currentMovie) => currentPromise.then(() => cutVideo(currentMovie)), Promise.resolve());
              

              Nếu duỗi ra thì nó như này:

              arrMovie.reduce((currentPromise, currentMovie) => {
                  return currentPromise.then(() => {
                      return cutVideo(currentMovie)
                  })
              }, Promise.resolve())
              

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

              N Duc Hoang 2 Replies Last reply Reply Quote 2
              • N
                Nguyen Quan @Vũ last edited by

                @Vũ Cảm ơn bạn. Cho mình hỏi làm sao để thêm cái phần đen đen như trong code của bạn? Mình ít post bài nên không biết.

                Vũ 1 Reply Last reply Reply Quote 0
                • Vũ
                  Vũ Global Moderator @Nguyen Quan last edited by Vũ

                  @Vũ said in Hỏi về cách chuyển ascii, html code thành text từ response json:

                  Hiện editor của forum sử dụng Markdown, bạn tham khảo về cú pháp của nó ở đây nhé. Viết rất dễ và đơn giản 😃

                  https://www.techmaster.vn/posts/33367/cu-phap-markdown-la-gi
                  https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet
                  https://vi.wikipedia.org/wiki/Markdown

                  Nhiều bạn hỏi cái này, nó gọi là code highlight. Hiện giờ markdown rất phổ biến, bạn cũng nên vọc nó xem. Nhất là khi làm việc với github.com

                  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
                  • Duc Hoang
                    Duc Hoang @Vũ last edited by

                    @Vũ cám ơn bạn. Mình viết cả return vào vì sợ bạn hỏi biết đâu chưa quen với các syntax rút gọn của arrow function nên có thể khó hiểu với cách làm này thôi 😃

                    N 1 Reply Last reply Reply Quote 2
                    • N
                      Nguyen Quan @Duc Hoang last edited by

                      @Duc-Hoang Code quá gọn và tiện dụng, không cần phải sử dụng thêm thư viện đúng như cách mình cần, cám ơn bạn nhiều.

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