'响应'在哪里?来自Google Drive API的JavaScript示例代码?

时间:2017-04-25 09:33:51

标签: jquery google-api promise google-drive-api google-javascript-api

我研究了这个页面(https://developers.google.com/drive/v3/web/quickstart/js) 并找到了如下代码:

/**
* Print files.
*/    
function listFiles() {
        gapi.client.drive.files.list({
            'pageSize': 10,
            'fields': "nextPageToken, files(id, name)"
        }).then(function(response) {
            appendPre('Files:');
            var returnedFiles = response.result.files;
            alert(returnedFiles);
            if (returnedFiles && returnedFiles.length > 0) {
                for (var i = 0; i < returnedFiles.length; i++) {
                    var file = returnedFiles[i];
                    alert(file);
                    appendPre(file.name + ' (' + file.id + ')');
                }
            } else {
                appendPre('No files found.');
            }
        });
    }

我想知道&#34;响应&#34; 在功能代码中来自何处?

}).then(function(response) {

以下行中的&#34;响应&#34; 是否相同?

Line1:  }).then(function(response) {

Line2:  var returnedFiles = response.result.files;

1 个答案:

答案 0 :(得分:1)

gapi.client.drive.files.list(...)返回承诺。如果您是Promises的新手,请参阅https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise

承诺使用then(f)对象解析(response位),而result对象又包含 // Setup the media library to load only photos, don't include other source types. let options: [String : AnyObject] = [MLMediaLoadSourceTypesKey: MLMediaSourceType.image.rawValue as AnyObject, MLMediaLoadIncludeSourcesKey: [MLMediaSourcePhotosIdentifier, MLMediaSourceiPhotoIdentifier] as AnyObject] // Create our media library instance to get our photo. mediaLibrary = MLMediaLibrary(options: options) 对象,该对象在https://developers.google.com/drive/v3/reference/files/list定义