Javascript: ожидание в цикле «for .. in» для объекта, не ожидающего асинхронного

Пакет Guava com.google.common.io может помочь вам немного. Следующее - нескомпилированное и непроверенное, но должно поместить вас на правильный путь.

long total = file1.length();
long progress = 0;
final OutputStream out = new FileOutputStream(file2);
boolean success = false;
try {
  ByteStreams.readBytes(Files.newInputStreamSupplier(file1),
      new ByteProcessor() {
        public boolean processBytes(byte[] buffer, int offset, int length)
            throws IOException {
          out.write(buffer, offset, length);
          progress += length;
          updateProgressBar((double) progress / total);
          // or only update it periodically, if you prefer
        }
        public Void getResult() {
          return null;
        }
      });
  success = true;
} finally {
  Closeables.close(out, !success);
}

Это может показаться большим количеством кода, но я считаю, что это будет наименее, с чего вам это удастся. (обратите внимание, что другие ответы на этот вопрос не дают примеров кода complete , поэтому их сложно сравнивать).

0
задан threxx 18 January 2019 в 10:49
поделиться

1 ответ

Вы не возвращаете обещание из createPage, поэтому await не может ждать.

Вы вернули созданную цепочку Обещаний с wp.pages().create:

function createPage(title, content) {
  var wp = new WPAPI({
    endpoint: 'http://localhost:8888/wordpress/wp-json',
    username: 'admin',
    password: 'pass'
  });

  return wp.pages().create({
      title: title,
      content: content,
      status: 'publish'
    })
    .catch(error => {
      console.error('Error: ' + error.message);
    })
    .then(function(response) {
      console.log(response.id);
      return response.id;
    })
}
.
0
ответ дан t.niese 18 January 2019 в 10:49
поделиться
Другие вопросы по тегам:

Похожие вопросы: