module.js
var Promise = require('bluebird');
var XMLHttpRequest = require('xhr2');
function fetchdata(id) {
var url = 'http://www.youtube.com/watch?v=' + id;
return new Promise(function (fulfill, reject) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = function() {
var jsonStr;
try {
fulfill(xhr.response);
} catch (e) {
reject(jsonStr);
}
};
xhr.onerror = function(e) {
reject(e);
};
xhr.send('');
});
}
module.exports = {
getdata: function (videoID) {
return new Promise(function (fulfill, reject) {
if (!videoID) {
reject(new Error('Unable to get video id.'));
return;
}
fetchdata(videoID).then(
function (d) {
console.log( d);
}
);
});
}
};
index.js
var parser = require('./module.js');
parser.getdata("ZI4tRn4dOGg", function (data) {
console.log(data);
}
)
我尝试使用xmlhttprequest获取youtube视图页面源代码。 但是以上代码未完成。我想它正在等待某事。 是蓝鸟的问题吗?还是xhr2?为什么这段代码永远无法完成?
答案 0 :(得分:1)
您的if (ContextCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
requestPermissions(arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION),1)
}else{
locationManager!!.requestLocationUpdates(LocationManager.GPS_PROVIDER,2,2f,locationListener)
mMap.clear()
var lastLocation = locationManager!!.getLastKnownLocation(LocationManager.GPS_PROVIDER)
var lastUserLocation = LatLng(lastLocation!!.latitude,lastLocation!!.longitude)
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(lastUserLocation,17f))
}
实例发生内存泄漏,可能是库的问题,上次发布是在一年前。 xhr
还可以。您可以使用Bluebird
并使用node-fetch
fetchdata